请教一个随机数的问题,谢谢
在程序中按照以下方式产生的随机数没有问题:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void 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. */\
int tmp;
double tempdouble;
for( i = 0; i < 1000;i++ )
{
tmp = rand();
printf( " %6d\n", tmp );
tempdouble = ((double)tmp)/RAND_MAX;
printf("tempdouble =%10.24f\n", tempdouble);
}
}
但是换成以下的程序
...
srand((unsigned)time( NULL));
while(pointCount < m_triangleNum)
{
float r1 = ((float)(rand()))/RAND_MAX;
TRACE("The m_index is %10.24f\n", r1);
...
}
的时候,在调试的时候出现的随机数都是一样的,如下所示:
The m_index is 0.505325479903561470000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
The m_index is 0.152867213965269950000000
...(后面的数据都是一样的)
为什么在这里产生的随机数就不是随机的了?急盼您的回答,万分感谢!