Is there a function to generate a random int number in C? (是否有在C中生成随机int数的函数?) Or will I have to use a third party library? (或者我必须使用第三方库吗?)
Note : Don't use rand() for security. (注意 :不要使用rand()来保证安全性。) If you need a cryptographically secure number, see this answer instead. (如果您需要加密安全号码, 请参阅此答案 。)
rand()
#include <time.h> #include <stdlib.h> srand(time(NULL)); // Initialization, should only be called once. int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.
Edit : On Linux, you might prefer to use random and srandom . (编辑 :在Linux上,您可能更喜欢使用random和srandom 。)
2.1m questions
2.1m answers
60 comments
57.0k users