随机数
时间:2009-07-18 来源:tancotq
rand()返回一个 0- RAND_MAX(2147483647)之间的随机数
产生一个 X , X >=0, X<1, 可用rand()/(RAND_MAX + 1.0)
以下是个简单的程序:
#include <stdlib.h>
#include <stdio.h>
#include <time.h> int main()
{
int i, j; srand((int)time(NULL));
for (i = 0; i < 10; i++)
{
j = 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
printf("%d ", j);
}
printf("\n");
}
编译: gcc 3-3.c -o 3-3 [root@localhost math]# ./3-3
3 5 5 8 1 6 10 7 1 4
[root@localhost math]# ./3-3
9 8 4 3 9 3 1 6 5 6
[root@localhost math]# ./3-3
7 9 9 4 5 9 9 1 5 6
[root@localhost math]#
#include <stdio.h>
#include <time.h> int main()
{
int i, j; srand((int)time(NULL));
for (i = 0; i < 10; i++)
{
j = 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
printf("%d ", j);
}
printf("\n");
}
编译: gcc 3-3.c -o 3-3 [root@localhost math]# ./3-3
3 5 5 8 1 6 10 7 1 4
[root@localhost math]# ./3-3
9 8 4 3 9 3 1 6 5 6
[root@localhost math]# ./3-3
7 9 9 4 5 9 9 1 5 6
[root@localhost math]#
相关阅读 更多 +