有一个函数可以在C中生成一个随机数吗?或者我将不得不使用第三方库?
如果你需要安全的随机字符或整数:
例如:
#include "sodium.h"
int foo()
{
char myString[32];
uint32_t myInt;
/* myString will be an array of 32 random bytes, not null-terminated */
randombytes_buf(myString, 32);
/* myInt will be a random number between 0 and 9 */
myInt = randombytes_uniform(10);
}
randombytes_uniform() 函数是比较安全的
#include <time.h>
#include <stdlib.h>
srand(time(NULL)); // should only be called once
int r = rand(); // returns a pseudo-random integer between 0 and RAND_MAX