Recommended way to initialize srand?

前端 未结 15 1656
夕颜
夕颜 2020-11-22 08:07

I need a \'good\' way to initialize the pseudo-random number generator in C++. I\'ve found an article that states:

In order to generate random-like

相关标签:
15条回答
  • 2020-11-22 08:42

    This is what I've used for small command line programs that can be run frequently (multiple times a second):

    unsigned long seed = mix(clock(), time(NULL), getpid());
    

    Where mix is:

    // http://www.concentric.net/~Ttwang/tech/inthash.htm
    unsigned long mix(unsigned long a, unsigned long b, unsigned long c)
    {
        a=a-b;  a=a-c;  a=a^(c >> 13);
        b=b-c;  b=b-a;  b=b^(a << 8);
        c=c-a;  c=c-b;  c=c^(b >> 13);
        a=a-b;  a=a-c;  a=a^(c >> 12);
        b=b-c;  b=b-a;  b=b^(a << 16);
        c=c-a;  c=c-b;  c=c^(b >> 5);
        a=a-b;  a=a-c;  a=a^(c >> 3);
        b=b-c;  b=b-a;  b=b^(a << 10);
        c=c-a;  c=c-b;  c=c^(b >> 15);
        return c;
    }
    
    0 讨论(0)
  • 2020-11-22 08:43

    On windows:

    srand(GetTickCount());
    

    provides a better seed than time() since its in milliseconds.

    0 讨论(0)
  • 2020-11-22 08:44
    #include <stdio.h>
    #include <sys/time.h>
    main()
    {
         struct timeval tv;
         gettimeofday(&tv,NULL);
         printf("%d\n",  tv.tv_usec);
         return 0;
    }
    

    tv.tv_usec is in microseconds. This should be acceptable seed.

    0 讨论(0)
  • 2020-11-22 08:47

    i suggest you see unix_random.c file in mozilla code. ( guess it is mozilla/security/freebl/ ...) it should be in freebl library.

    there it uses system call info ( like pwd, netstat ....) to generate noise for the random number;it is written to support most of the platforms (which can gain me bonus point :D ).

    0 讨论(0)
  • 2020-11-22 08:48

    Include the header at the top of your program, and write:

    srand(time(NULL));
    

    In your program before you declare your random number. Here is an example of a program that prints a random number between one and ten:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
       //Initialize srand
       srand(time(NULL));
    
       //Create random number
       int n = rand() % 10 + 1;
    
       //Print the number
       cout << n << endl; //End the line
    
       //The main function is an int, so it must return a value
       return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 08:49

    The best answer is to use the Boost random number stuff. Or if you have access to C++11 use the <random> header.

    But if we are talking about rand() and srand()
    The best way is just to use time():

    int main()
    {
        srand(time(NULL));
    
        ...
    }
    

    Be sure to do this at the beginning of your program, and not every time you call rand()!

    Every time you start up, time() will return a unique value (unless you start the application multiple times a second). In 32 bit systems, it will only repeat every 60 years or so.

    I know you don't think time is unique enough but I find that hard to believe. But I have been known to be wrong.

    If you are starting a lot of copies of your application simultaneously you could use a timer with a finer resolution. But then you run the risk of a shorter time period before the value repeats.

    OK, so if you really think you are starting multiple applications a second.
    Then use a finer grain on the timer.

     int main()
     {
         struct timeval time; 
         gettimeofday(&time,NULL);
    
         // microsecond has 1 000 000
         // Assuming you did not need quite that accuracy
         // Also do not assume the system clock has that accuracy.
         srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
    
         // The trouble here is that the seed will repeat every
         // 24 days or so.
    
         // If you use 100 (rather than 1000) the seed repeats every 248 days.
    
         // Do not make the MISTAKE of using just the tv_usec
         // This will mean your seed repeats every second.
     }
    
    0 讨论(0)
提交回复
热议问题