Thread: Good Random Number Generator

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002

    Good Random Number Generator


    Im using the Monte Carlo method to get an estimate of Pi.

    I generate numbers between 0.0 and 1.0, using the following function (similar to the one in the FAQ)

    double generate_rand(double min, double max)
      static int Init = 0;
      double rc;
      if (Init == 0)
        Init = 1;
       rc =  (rand() / (RAND_MAX + 1.0) * (max - min) + min);
       return rc;
    The estimation of pi depends on the quality of the random number generator, I was wondering if there is any way to make my function generate 'better' random numbers?

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    >I was wondering if there is any way to make my function generate 'better' random numbers? ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2003
    You could use a 64bit random number generator. You can build one by using 64-bit numbers and bit-shifting 24-bit random digits from rand().

  4. #4
    Registered User
    Join Date
    Jun 2004
    You could combine rand with XORs. As more, as better.
    int cnt;
    int res = rand();
        res = res ^ rand();
    But wouldn't be any good algorithm... just to clean your hand from time to time
    Last edited by xErath; 11-17-2004 at 09:54 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Take a look at the Mersenne Twister pseudo-random number generator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Class
    By abachler in forum Windows Programming
    Replies: 4
    Last Post: 09-03-2008, 08:30 AM
  2. Random number generator
    By rehan in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2008, 02:14 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM