我这样来实现的,因为我要的是0——1之间的随机数,你可以用我的方法实现,产生的随机0和1! double number = rand()/32767.0;//产生随机数 int num = (int)(((number * 10) + 5) / 10);
int i; time_t t; srand((unsigned) time(&t)); i = rand();
其实使用srand(rand());也不错哦
int i; time_t t; srand((unsigned) time(&t)); i = rand(); 什么代码?有必要吗?
一下代码摘自msdn Example// crt_rand.c /* This program seeds the random-number generator * with the time, then displays 10 random integers. */#include <stdlib.h> #include <stdio.h> #include <time.h>int main( void ) { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); }Sample Output 19430 28222 9710 12070 7513 9501 1767 26041 11872 4097
double number = rand()/32767.0;//产生随机数
int num = (int)(((number * 10) + 5) / 10);
time_t t;
srand((unsigned) time(&t));
i = rand();
time_t t;
srand((unsigned) time(&t));
i = rand();
什么代码?有必要吗?
Example// crt_rand.c
/* This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/#include <stdlib.h>
#include <stdio.h>
#include <time.h>int main( void )
{
int i; /* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}Sample Output 19430
28222
9710
12070
7513
9501
1767
26041
11872
4097