C++ Random Number: What are good random number generators?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: C++ Random Number: What are good random number generators?

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

    C++ Random Number: What are good random number generators?

    Q: What are good random number generators?

    A: There are two common problems when using pseudo-random number generators (PRNGs):
    • The PRNG does not get seeded. Please see related FAQ for discussion on this topic.
    • A poor PRNG is being used.

    This FAQ addresses the second problem.

    Often people use the C library function 'rand()' for random number generation. And quite often, the implementation for 'rand()' is not very good - the range of numbers is too small, the period before repetition is too small, and/or other problems.

    Better PRNGs can be found in a variety of sources. The most popular is the "Mersenne Twister". The "Mersenne Twister" and other good PNRGs can be found in the Boost library and the GNU Scientific Library).

    Another good resource on random number generation is the book "Numerical Recipes in C". It contains a very good discussion on how one can create different distributions based on a uniform random deviate.


    Last edited by Andreas Masur; July 24th, 2005 at 05:48 AM.

  2. #2
    Join Date
    Apr 2004
    Posts
    76

    C++ Random Number: What are good random number generators?

    Additionally, Wei Dai provides PRNG in his Crypto++ library located at www.cryptopp.com.

    Typical code would look as follows:

    Code:
    #include "osrng.h"
    
       using namespace CryptoPP;
       .
       .
       .
       AutoSeededRandomPool rng;
       byte randomBytes[10];
       rng.GenerateBlock(randomBytes, 10);
    Otherwise you should use RandomPool and seed it yourself with random, unpredictable data:

    Code:
       RandomPool rng;
       rng.Put(seed, seedLen);
       byte randomBytes[10];
       rng.GenerateBlock(randomBytes, 10);

    Last edited by Andreas Masur; July 24th, 2005 at 05:48 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center