Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 303
Cistoran Avatar asked May 01 '11 21:05

Cistoran


People also ask

What is the lucky number 1 to 50?

1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99, 105, 111, 115, 127, 129, 133, 135, 141, 151, 159, 163, 169, 171, 189, 193, 195, 201, 205, 211, 219, 223, 231, 235, 237, 241, 259, 261, 267, 273, 283, 285, 289, 297, 303, 307, 319, 321, 327, ...

Can you trick a random number generator?

As you can see, it is completely possible to hack an RNG that's based on a computer program like the ones used in casinos and online games. That's not to say, however, that it is easy. These companies spend a pretty penny to make sure that their games are secure with extensive protocols installed.

Can Google pick a random number?

Ask Google to generate a random number As with most Google searches, you can either first head to Google's website or make sure your web browser's URL bar is set to use the Google search engine. Next, type in random number or random number generator.


2 Answers

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.

like image 73
Jason Moore Avatar answered Sep 20 '22 05:09

Jason Moore


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

like image 31
Jacob Avatar answered Sep 19 '22 05:09

Jacob