请问vc里有没有用来产生随机序列的函数,或者谁能帮忙提供一个这方面的算法,谢谢!!

解决方案 »

  1.   

    1. 标准C,如下MSDN例
    /* 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>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. */
       for( i = 0;   i < 10;i++ )
          printf( "  %6d\n", rand() );
    }
    2. 更好的随机数,见 MSDN Platform SDK: Security    CryptGenRandom()例
    The following code snippet shows the generation of 8 random bytes. These can be used to create cryptographic keys or for any application that uses random numbers.//--------------------------------------------------------------------
    // Declare and initialize variables.HCRYPTPROV   hCryptProv;
    BYTE         pbData[16];//--------------------------------------------------------------------
    //  This code assumes that a cryptographic context has been acquired 
    //  For code details, see "Example C Program: Duplicating a Session 
    //  Key."//--------------------------------------------------------------------
    // Generate a random initialization vector.if(CryptGenRandom(
       hCryptProv, 
       8, 
       pbData)) 
    {
         printf("Random sequence generated. \n");
    }
    else
    {
         printf("Error during CryptGenRandom.\n");
         exit(1);
    }
      

  2.   

    t=CTime::GetCurrentTime();
    t2=t.GetSecond();
    x=t2+rand();
      

  3.   

    昨天我才搜索了一下随机数的产生问题,找到了一个很好的方法,如下:
    #include "stdlib"
    CTimer t=CTimer::GetCurrentTime();
    (rand()+t.GetSecond())%最大正整数(注意整数字长)
    这是我拷贝的。可能在C语言中用,我没有试,能不能行,我不知道。VC下我改了一下:
    CTime t=CTime::GetCurrentTime();
    m_random = (rand() + t.GetSecond())%m_range;
    m_range为产生随机数的最大值,生成的是0--m_range之间的随机数,这代码我用了的,效果不错,试试吧!