在程序中按照以下方式产生的随机数没有问题:
#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
...(后面的数据都是一样的)
为什么在这里产生的随机数就不是随机的了?急盼您的回答,万分感谢!

解决方案 »

  1.   

    将float r1 = ((float)(rand()))/RAND_MAX;改成double r1 = ((double)(rand()))/RAND_MAX;产生的数据还是一样的。谢谢!
      

  2.   

    srand((unsigned)time( NULL));
     while(pointCount < m_triangleNum)
     {
         float r1 = ((float)(rand()))/RAND_MAX;
         TRACE("The m_index is %10.24f\n", r1);
         float ran = r1 * totalArea;
         int m_index = FindTriangleIndex(sumArea, ran);
         //TRACE("The m_index is %d\n", m_index);
         CTriangle sampleTriangle = m_triangleList.GetAt(m_triangleList.FindIndex(m_index));
         if(pointCount == 0)
        {
    m_samplePoint.AddTail(*(sampleTriangle.GetSamplingPoint()));
        }
        else
        {
             m_samplePoint.InsertAfter(m_samplePoint.GetTailPosition(), *(sampleTriangle.GetSamplingPoint()));
         }
         pointCount++;
     }这就是产生一致随机数的完整代码,其他地方好像关系不大啊,我在调试的时候产生的随机数都不是真正的随机数,如果将断点设置在TRACE()语句上,一般产生5~6个相同的数后就会产生另一个差别不大的数,但是这个数又得重复5~6次才是下一个数,如果将F5按慢一点,产生的数倒是不同,但是还是不是随机的,看上去倒像一个递增的序列。如果将断点放在别的地方,在调试的时候输出的就像上面的那样了。谢谢!
      

  3.   

    srand(timeGetTime() )改成这个..
      

  4.   

    不行啊,改成srand(timeGetTime())还是没有什么变化啊,但是在运行我贴的第一个程序的时候产生的数确实是随机数啊,不知道为什么一放到别的程序里面,运行的结果就不对了。谢谢!
      

  5.   

    是不是跟TRACE()语句有什么关系啊?
      

  6.   

    while(pointCount < m_triangleNum)
     {
       sleep(1)
       srand(timeGetTime() )
       int a=rand()%xxx
      }
      

  7.   

    你的程序在我这儿运行,结果很好: (100个随机数)
    The m_index is 0.443952757347331140000000
    The m_index is 0.329264198736533730000000
    The m_index is 0.369151890621662040000000
    The m_index is 0.600970488601336730000000
    The m_index is 0.033997619556260870000000
    The m_index is 0.300088503677480410000000
    The m_index is 0.454298532059694230000000
    The m_index is 0.690664387951292480000000
    The m_index is 0.716208380382702070000000
    The m_index is 0.441053498947111430000000
    The m_index is 0.345927304910428170000000
    The m_index is 0.441236610003967410000000
    The m_index is 0.080477309488204599000000
    The m_index is 0.822443311868648390000000
    The m_index is 0.812280648213141280000000
    The m_index is 0.111239967040009770000000
    The m_index is 0.162297433393353060000000
    The m_index is 0.404828028199102740000000
    The m_index is 0.270729697561571110000000
    The m_index is 0.050294503616443373000000
    The m_index is 0.606707968382824150000000
    The m_index is 0.671681875057222210000000
    The m_index is 0.515854365672780530000000
    The m_index is 0.641102328562273010000000
    The m_index is 0.467268898586993000000000
    The m_index is 0.357219153416547140000000
    The m_index is 0.159611804559465300000000
    The m_index is 0.102755821405682540000000
    The m_index is 0.421033356730857290000000
    The m_index is 0.926847132786034700000000
    The m_index is 0.816278572954496910000000
    The m_index is 0.061799981688894313000000
    The m_index is 0.680837427900021330000000
    The m_index is 0.965178380687887190000000
    The m_index is 0.562364574114200270000000
    The m_index is 0.296761986144596700000000
    The m_index is 0.386028626361888500000000
    The m_index is 0.128971221045564130000000
    The m_index is 0.703543198950163260000000
    The m_index is 0.563249610889004160000000
    The m_index is 0.752891628772850760000000
    The m_index is 0.820581682790612500000000
    The m_index is 0.379070406201361120000000
    The m_index is 0.904843287453840790000000
    The m_index is 0.766319772942289480000000
    The m_index is 0.238227484969634080000000
    The m_index is 0.035615100558488724000000
    The m_index is 0.356944486831263170000000
    The m_index is 0.693441572313608260000000
    The m_index is 0.959837641529587730000000
    The m_index is 0.301431318094424250000000
    The m_index is 0.449476607562486640000000
    The m_index is 0.201086458937345500000000
    The m_index is 0.715872676778466110000000
    The m_index is 0.292062135685293160000000
    The m_index is 0.414288766136661880000000
    The m_index is 0.357799005096591070000000
    The m_index is 0.508285775322733220000000
    The m_index is 0.879146702475051110000000
    The m_index is 0.504287850581377590000000
    The m_index is 0.301889095736564250000000
    The m_index is 0.731192968535416710000000
    The m_index is 0.953062532425916320000000
    The m_index is 0.329844050416577660000000
    The m_index is 0.856837672048097130000000
    The m_index is 0.449781792657246600000000
    The m_index is 0.211340678121280550000000
    The m_index is 0.619403668324839060000000
    The m_index is 0.875179296243171480000000
    The m_index is 0.493758964812158590000000
    The m_index is 0.503341776787621710000000
    The m_index is 0.309640797143467540000000
    The m_index is 0.592059083834345580000000
    The m_index is 0.521286660359507990000000
    The m_index is 0.263069551683095810000000
    The m_index is 0.372478408154545750000000
    The m_index is 0.769981994079409150000000
    The m_index is 0.710257271034882680000000
    The m_index is 0.061006500442518388000000
    The m_index is 0.284371471297341860000000
    The m_index is 0.915738395336771700000000
    The m_index is 0.232306894131290630000000
    The m_index is 0.806573686941129740000000
    The m_index is 0.544480727561265860000000
    The m_index is 0.350657673879207770000000
    The m_index is 0.166661580248420680000000
    The m_index is 0.631611072115237930000000
    The m_index is 0.147831659901730390000000
    The m_index is 0.416119876705221710000000
    The m_index is 0.009430219428083133300000
    The m_index is 0.987731559190649140000000
    The m_index is 0.420239875484481330000000
    The m_index is 0.594470046082949290000000
    The m_index is 0.435132908108767980000000
    The m_index is 0.986785485396893260000000
    The m_index is 0.120090334788048950000000
    The m_index is 0.827631458479567870000000
    The m_index is 0.465987121189001150000000
    The m_index is 0.517502365184484340000000
    The m_index is 0.214575640125736270000000
      

  8.   

    难道srand跟CPU有关系吗?你是什么CPU啊
      

  9.   

    不会吧,在我这里怎么会是这样呢,我的CPU是奔四的呀,郁闷!还是很感谢!
      

  10.   

    我自己写过一个随机发生器,为的是产生密码用的。因为srand的种子依赖于系统时间,
    再说种子确定了,那么那串随机数就确定了,这样产生密码不安全。
    要不我给你我的?
      

  11.   

    谢谢cut9(切瓜玩),我现在只需要产生0~1之间的随机数,利用这个随机数去提取三维模型中的一些特征点。因为这个随机数产生的不好,导致最后提取出来的特征点都集中在某一个固定的区域,根本反应不出来三维模型的特征。你的那个能产生出0~1的随机数么?
      

  12.   

    按照lzd(活着便精彩)的提议,我修改了一下(我的程序Sleep(6)),产生的数据如下:
    The m_index is 0.470412305063020730000000
    The m_index is 0.472090823084200580000000
    The m_index is 0.472914822840052480000000
    The m_index is 0.473586230048524400000000
    The m_index is 0.474288155766472370000000
    The m_index is 0.474990081484420290000000
    The m_index is 0.475600451673940270000000
    The m_index is 0.476302377391888190000000
    The m_index is 0.476973784600360110000000
    The m_index is 0.477675710318308080000000
    The m_index is 0.478774376659443950000000
    The m_index is 0.479476302377391860000000
    The m_index is 0.480178228095339830000000
    The m_index is 0.480880153813287750000000
    The m_index is 0.481582079531235720000000
    The m_index is 0.482284005249183630000000
    The m_index is 0.482955412457655550000000
    The m_index is 0.483565782647175530000000
    The m_index is 0.484267708365123450000000
    The m_index is 0.485366374706259370000000
    The m_index is 0.486068300424207290000000
    The m_index is 0.486739707632679210000000
    The m_index is 0.487441633350627180000000
    The m_index is 0.488143559068575090000000
    The m_index is 0.488845484786523010000000
    The m_index is 0.489547410504470980000000
    The m_index is 0.490249336222418900000000
    The m_index is 0.490951261940366810000000
    The m_index is 0.491622669148838790000000
    The m_index is 0.492233039338358710000000
    The m_index is 0.492934965056306630000000
    The m_index is 0.493636890774254600000000
    The m_index is 0.494338816492202520000000
    The m_index is 0.495040742210150430000000
    The m_index is 0.495712149418622410000000
    The m_index is 0.496322519608142330000000
    The m_index is 0.497024445326090250000000
    The m_index is 0.497726371044038220000000
    The m_index is 0.498428296761986130000000
    The m_index is 0.499099703970458110000000
    The m_index is 0.499801629688406020000000
    The m_index is 0.500411999877925950000000
    The m_index is 0.501113925595873870000000
    The m_index is 0.501815851313821780000000
    The m_index is 0.502487258522293810000000
    The m_index is 0.503585924863429680000000
    The m_index is 0.504287850581377590000000
    The m_index is 0.504898220770897520000000
    The m_index is 0.505600146488845440000000
    The m_index is 0.506302072206793460000000
    The m_index is 0.506973479415265380000000
    The m_index is 0.507675405133213300000000
    The m_index is 0.508377330851161210000000
    The m_index is 0.508987701040681140000000
    The m_index is 0.509689626758629060000000
    The m_index is 0.510361033967101090000000
    The m_index is 0.511062959685049000000000
    The m_index is 0.511764885402996920000000
    ...
    这个看上去也不是一个随机的数列啊。十分感谢!
      

  13.   

    改成srand((unsigned)GetTickCount());试试?
      

  14.   

    我也认为Sleep()不好,但是即使Sleep()了,产生的数据还是不是随机的数,看上去更像一个递增的序列。郁闷