Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a random number generator work?

Tags:

random

How do random number generator works? (for example in C/C++ Java)

How can I write my own random number generator? (for example in C/C++ Java)

like image 883
Rella Avatar asked Nov 11 '09 16:11

Rella


People also ask

Do random number generators have a pattern?

But good random number generators don't have any clear pattern to their output, making finding which page of their codebook they correspond to very difficult.) There is no limit to the size of the codebook that algorithmic random number generation can support.

How do you generate random numbers?

Computers can generate truly random numbers by observing some outside data, like mouse movements or fan noise, which is not predictable, and creating data from it. This is known as entropy. Other times, they generate “pseudorandom” numbers by using an algorithm so the results appear random, even though they aren't.

Is a random number generator really random?

They are not truly random because the computer uses an algorithm based on a distribution, and are not secure because they rely on deterministic, predictable algorithms. Since a seed number can be set to replicate the “random” numbers generated, it is possible to predict the numbers if the seed is known.

Can a random number generator be manipulated?

With some random number generators, it's possible to select the seed carefully to manipulate the output. Sometimes this is easy to do. Sometimes it's hard but doable. Sometimes it's theoretically possible but practically impossible.


6 Answers

There is also this algorithm:

enter image description here

Oh, and more seriously:

Random number generators use mathematical formulas that transfer set of numbers to another one. If, for example, you take a constant number N and another number n_0, and then take the value of n mod N (the modulo operator), you will get a new number n_1, which looks as it if is unrelated to n_0. Now, repeat the same process with n_1 and you'll get another number. What you have here is a (VERY BAD) generator of seemingly random numbers.

Remember, the method I've described here is a toy method that should not be used for anything serious. It does, however, illustrate the general principle.

Also note:

If all scientific papers whose results are in doubt because of bad rands were to disappear from library shelves, there would be a gap on each shelf about as big as your fist.

(paraphrased from chapter 7 of Numerical recipes). This is a must-read text for anyone who uses random number generators for any serious work.

like image 166
Boris Gorelik Avatar answered Oct 03 '22 12:10

Boris Gorelik


One of the things to keep in mind is that there are no 'true' random number generators. They just generate numbers that look random to us mere mortals.

One of the easiest examples of this (to implement, as well) is the Linear congruential generator. Sure, the numbers look unpredictable to you and me, but they're actually evenly spaced within a finite field.

Of course, some generators, like Blum Blum Shub aren't predictable for an outsider even if he applies serious mathematical skills and computing power to the task, but at the fundamental level, random number generators aren't random; they're regular and predictable.

like image 30
Michiel Buddingh Avatar answered Oct 03 '22 14:10

Michiel Buddingh


How I made them in the old days was by getting some value from the system that changes really rapidly, for example the system millisecond timer.

The next thing you have to do is to apply some formula that will generate a new number from this "input" number and clip it to the range you need, eg 0..255:

random_number = integer(formula(timer-value)) MOD 255

That way, you have a new "random" number every time you call the function.

An example formula function could be:
formula(x) = ((x XOR constant) + constant2) MOD range
XOR used to be one of my favourites.


Update: I realize that this formula is a very bad one, it generates a pretty predictable set of numbers. Also, the system timer is too predictable as a source. So for most applications, this does not suffice. If you need better randomness, use more sources than just the system timer and better formulas to combine them.

like image 22
littlegreen Avatar answered Oct 03 '22 12:10

littlegreen


I found this one for Java:

http://www.javamex.com/tutorials/random_numbers/java_util_random_algorithm.shtml

by googling how random functions work java

I'm sure the answer is language-specific, but you can try altering my Google query for the language of your choice.

like image 21
David Avatar answered Oct 03 '22 12:10

David


There is a lot of information available about how they are working ... see Konamiman's answer and use google a bit.

Why would you like to write a new random generator? You probably should not try to do so ... until you need something very special. For example in a game you could use a shuffle bag which produces 'fair' random values - have a look at this interesting question on SO.
I post this here, because I really liked the idea and implementation when I read about it the first time :)

like image 37
tanascius Avatar answered Oct 03 '22 12:10

tanascius


It's also common to seed these types of generators by using something like the fifth decimal of the current clock time which also appears random to us. Add together inputs from a few chaotic systems, e.g. weather data, stock market data, again, using modulus and you have good seed numbers.

like image 25
user3918295 Avatar answered Oct 03 '22 13:10

user3918295