srand()和rand()
声明
#include <stdlib.h>
int rand(void);
int rand_r(unsigned int *seedp); //线程安全版本
void srand(unsigned int seed);
实例
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
for(int i = 0; i < 10;i++ )
printf("%d = %u\n",i,rand());
return 0;
}
二次随机
srand((unsigned)time(NULL));
int RanMe1 = rand() % 1000000;
srand(RanMe1);
int RanMe2 = rand() % 1000000;
srandom()和random()
声明
#include <stdlib.h>
long int random(void);
void srandom(unsigned int seed);
int random_r(struct random_data *buf, int32_t *result);
int srandom_r(unsigned int seed, struct random_data *buf);
实例
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
srandom(time(0));
for(int i = 0; i < 10;i++ )
printf("%d = %u\n",i,random());
return 0;
}