C++ Random Number: Why does my random number generator always return the same set?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: C++ Random Number: Why does my random number generator always return the same set?

  1. #1
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    C++ Random Number: Why does my random number generator always return the same set?

    Q: Why does my random number generator always return the same set?

    A: A random number generator needs to be seeded before use, or it will always generate the same list of random numbers. For example the C 'time()' function can be used for this:

    Code:
    srand(time(NULL));
    One should generally avoid using 'clock()' to initialize random numbers, since 'clock()' returns the amount of time the current thread has been running, generally a number close to zero.

    Good seeds will involve more "random" bits. One way this can be accomplished is by using higher resolution timers such as a performance counter or an RDTSC value. Better seeds may mix the least significant bits of different timers. The best seeds are produced from truely random sources. If a computer is connected to the internet, truely random seeds can be obtained from sites like random.org.


    Last edited by Andreas Masur; July 24th, 2005 at 06:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center