关于C语言的函数问题请问,randomize()random这两
请问,randomize() random 这两个怎么用?他们的作用是什么? 请举例说明一下,谢谢!
名: rand 功 能: 随机数发生器 用 法: void rand(void); //产生0~32767的随机数 程序例: #include #include int main(void) { int i; printf("Ten random numbers from 0 to 99\n\n"); for(i=0; i<10; i++) printf("%d\n", rand() % 100); return 0; } 第一次运行结果: Ten random numbers from 0 to 99 46 30 82 90 56 17 95 15 48 26 第二次运行结果: Ten random numbers from 0 to 99 46 30 82 90 56 17 95 15 48 26 函数名: random 功 能: 随机数发生器 用 法: int random(int num); //产生num以内的随机数 程序例: #include #include #include /* prints a random number in the range 0 to 99 */ int main(void) { randomize(); printf("Random number in the 0-99 range: %d\n", random (100)); return 0; } 第一次运行结果:Random number in the 0-99 range:97 第二次运行结果:Random number in the 0-99 range:20 ...... 函数名: randomize 功 能: 初始化随机数发生器 用 法: void randomize(void); 程序例: #include #include #include int main(void) { int i; randomize(); printf("Ten random numbers from 0 to 99\n\n"); for(i=0; i<10; i++) printf("%d\n", rand() % 100); return 0; } 第一次运行结果: Ten random numbers from 0 to 99 14 68 14 99 91 90 12 13 21 16 第二次运行结果: Ten random numbers from 0 to 99 71 44 26 87 98 45 44 16 57 79 ...... 说明:rand函数,是产生0-32767之间的随机整数的。 所以用rand()%n就可以生产0到n-1之间的随机整数。 若想得到每次不同的结果,可以将系统时间作为randomize的参数来初始化,只要放在rand函数之前就行了。