Cistoran
Cistoran

Reputation: 1597

Reinventing The Wheel: Random Number Generator

So I'm new to C++ and am trying to learn some things. As such I am trying to make a Random Number Generator (RNG or PRNG if you will). I have basic knowledge of RNGs, like you have to start with a seed and then send the seed through the algorithm. What I'm stuck at is how people come up with said algorithms.

Here is the code I have to get the seed.

int getSeed()
{
    time_t randSeed;
    randSeed = time(NULL);
    return randSeed;
}

Now I know that there is are prebuilt RNGs in C++ but I'm looking to learn not just copy other people's work and try to figure it out.

So if anyone could lead me to where I could read or show me examples of how to come up with algorithms for this I would be greatly appreciative.

Upvotes: 9

Views: 1996

Answers (4)

Martin
Martin

Reputation: 231

The correct solution best fulfills the requirements and the requirements of every situation will be unique. This is probably the simplest way to go about it:

  • Create a large one dimensional array populated with "real" random values.
  • "seed" your pseudo-random generator by calculating the starting index with system time.
  • Iterate through the array and return the value for each call to your function.
  • Wrap around when it reaches the end.

Upvotes: 0

Jason Moore
Jason Moore

Reputation: 3374

First, just to clarify, any algorithm you come up with will be a pseudo random number generator and not a true random number generator. Since you would be making an algorithm (i.e. writing a function, i.e. making a set of rules), the random number generator would have to eventually repeat itself or do something similar which would be non-random.

Examples of truly random number generators are one's that capture random noise from nature and digitize it. These include:

http://www.fourmilab.ch/hotbits/

http://www.random.org/

You can also buy physical equipment that generate white noise (or some other means on randomness) and digitally capture it:

http://www.lavarnd.org/

http://www.idquantique.com/true-random-number-generator/products-overview.html

http://www.araneus.fi/products-alea-eng.html

In terms of pseudo random number generators, the easiest ones to learn (and ones that an average lay person could probably make on their own) are the linear congruential generators. Unfortunately, these are also some of the worst PRNGs there are.

Some guidelines for determining what is a good PRNG include:

  1. Periodicity (what is the range of available numbers?)
  2. Consecutive numbers (what is the probability that the same number will be repeated twice in a row)
  3. Uniformity (Is it just as likely to pick numbers from a certain sub range as another sub range)
  4. Difficulty in reverse engineering it (If it is close to truly random then someone should not be able to figure out the next number it generates based on the last few numbers it generated)
  5. Speed (how fast can I generate a new number? Does it take 5 or 500 arithmetic operations)
  6. I'm sure there are others I am missing

One of the more popular ones right now that is considered good in most applications (ie not crptography) is the Mersenne Twister. As you can see from the link, it is a simple algorithm, perhaps only 30 lines of code. However trying to come up with those 20 or 30 lines of code from scratch takes a lot of brainpower and study of PRNGs. Usually the most famous algorithms are designed by a professor or industry professional that has studied PRNGs for decades.

I hope you do study PRNGs and try to roll your own (try Knuth's Art of Computer Programming or Numerical Recipes as a starting place), but I just wanted to lay this all out so at the end of the day (unless PRNGs will be your life's work) its much better to just use something someone else has come up with. Also, along those lines, I'd like to point out that historically compilers, spreadsheets, etc. don't use what most mathematicians consider good PRNGs so if you have a need for a high quality PRNGs don't use the standard library one in C++, Excel, .NET, Java, etc. until you have research what they are implementing it with.

Upvotes: 5

user2100815
user2100815

Reputation:

To quote John von Neumann:

Anyone who considers arithmetical methods of producing random digits is of course in a state of sin.

This is taken from Chapter 3 Random Numbers of Knuth's book "The Art of Computer Programming", which must be the most exhaustive overview of the subject available. And once you have read it, you will be exhausted. You will also know why you don't want to write your own random number generator.

Upvotes: 3

Jacob
Jacob

Reputation: 34601

A linear congruential generator is commonly used and the Wiki article explains it pretty well.

Upvotes: 3

Related Questions