请教: vc 中的如何取随机数的问题 ,
如我想在1-10 之间取随机数. 且精确到2位小数...
应如何解决.........
请高手指点...
解决后马上结贴!!!!!!!!!

解决方案 »

  1.   

    int rand( void );
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_rand.asp
      

  2.   

    srand (your_see) ;
    rand ();
    即可首先设置一个种子,种子可以为任意数,然后使用rand获得一个random数
      

  3.   

    回复楼上!srand(your_see) 有何作用!请写出详细的用法...
    多谢了!
      

  4.   

    srand()设置时间种子,看MSDN
    int value = rand() % 1000; //如果可以取到10.xx,那么就是%1100,返回不大于1100的非负整数
    float dest = value / 100.0;
      

  5.   

    真懒……
    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() );
    }
      

  6.   

    srand((unsigned)time(NULL));
    int i=rand()%10+1;
      

  7.   

    void RESULTDLG::OnButton2() 
    { double i=100+rand()%900;
    m_result.Format("%.2f",i/100.);
    UpdateData(FALSE);
    }
      

  8.   

    刚才没有看清楼主的问题         srand((unsigned)time(NULL));
    int i=rand()%10000+1;//1-10000的随机数
    float d=i/100.00;//根据自己的随机数位数决定
    CString str;
    str.Format("%.2f",d);//精确到两位
    AfxMessageBox(str);
      

  9.   

    多谢了...........好像rand() 产生的是随机数,如果基数设定一样,其结果是相同的序列...