我在VC7下面用了random和randomize两个函数,发现他们己从stdlib.h中被扔了出去,请问各位大侠有什么办法实现产生随机数的功能,请拉兄弟一把。

解决方案 »

  1.   

    random是VB中的,randomize是TC中的
    下面实现1-10之间的随机数#include <stdlib.h>
    #include <iostream.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 ) );
      double p=0;
       for( i = 0;   i < 100;i++ )
          {  p=rand()%10+1;
    cout<<p<<endl;
    }
      

  2.   

    srand( (unsigned)time( NULL ) ); //
    srand(rand());//
             int i = rand();
      

  3.   

    转贴
    #include <stdlib.h> /* header for rand() and srand() */
    #include <stdio.h> /* io header */
    #include <time.h>  /* header needed for time() */ 
    void main() 
    {             srand(time(0)); 
                for(int i=0;i<=100;i++)
                     printf("%d\n",rand()); 

    }
      

  4.   

    srand( (unsigned)time( NULL ) );
    int i = rand();
    你还可以控制随机数的范围!
    int i = rand()/%m+n;
    那么随机数i的范围是n----m+n;
      

  5.   

    srand( (unsigned)time( NULL ) );
    int i = rand();
    int x = 100;
    int i = rand()/x;
    那么随机数i的范围是0---99;
      

  6.   

    unsigned long dw1, dw2, dw3, dwMask;
           //srand(m_Index);
          for(i=0;i<n;i++)//换牌算法
      {
      dw3 = (dw1 + dw2) ^ dwMask;
          RandNum = dw3%Number+1;
          dw1 = dw2;dw2 = dw3;
    }
    Number是的随机数最大值
      

  7.   

    Example/* 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() );
    }
    Output    6929
        8026
       21987
       30734
       20587
        6699
       22034
       25051
        7988
       10104——摘自MSDN
      

  8.   

    用rand   srand  只能产生低等级的随机数。
    产生高级的请看http://sjdwy.533.net/zw/SJS.htm
      

  9.   

    这是MSDN 2002的解释:Sets a random starting point.void srand(
       unsigned int seed 
    );
    Parameters
    seed 
    Seed for random-number generation 
    Res
    The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.Requirements
    Routine Required header Compatibility 
    srand <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP For additional compatibility information, see Compatibility in the Introduction.LibrariesAll versions of the C run-time libraries.Example
    /* 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() );
    }Output
        6929
        8026
       21987
       30734
       20587
        6699
       22034
       25051
        7988
       10104See Also
    Floating-Point Support Routine | rand--------------------------------------------------------------------------------Send feedback to Microsoft&copy; 2001 Microsoft Corporation. All rights reserved.