一个简单的生成随机数的小程序

#include "stdio.h" #include "time.h" #include "math.h" static unsigned long rand_seed; void rand_init() { struct tm *tm1; time_t tp; time(&tp); tm1 = localtime((const*) &tp); rand_seed = (tm1->tm_mday*tm1->tm_hour*tm1->tm_hour-tm1->tm_min*tm1->tm_sec)%tm1->tm_sec; } unsigned long randnumber(unsigned long begin, unsigned end) { int x = 23; int y = 7; unsigned long rand_number; rand_init(); //printf("randseed is :%d/n", rand_seed); if(begin > end) return -1; if(begin == end) return(begin); while(1){ rand_number = (rand_seed * x + y)%(end + 1); if(rand_number >= begin && rand_number <= end) return(rand_number); } } int main() { unsigned long randn; randn = randnumber(1,100); if(randn == -1){ printf("randnumber is error/n"); return -1; } printf("%d/n", randn); scanf("%d", &randn); }

你可能感兴趣的:(C语言)