Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Random number

Akhilesh Trivedi
Ranch Hand
Posts: 1608
How it is generated by a machine?

Campbell Ritchie
Sheriff
Posts: 50749
83
There are all sorts of ways a machine could create a random number: thermal noise in a resistor, nuclear decay (recording the order of hits between two Geiger counters), rolling balls like a roulette wheel. Lots of others.
A computer cannot create a random number. If you look through the code for the java.util.Random class, by unzipping the src.zip file in your Java™ installation folder, you can see how the Random class attempts to get close to randomness.

Matthew Brown
Bartender
Posts: 4568
9
Have a search for "pseudo-random numbers". Basically, computers don't generally generate truly random numbers. Instead they use an algorithm that generates numbers that appear to be random, and are close enough to random for most purposes.

Akhilesh Trivedi
Ranch Hand
Posts: 1608

What is not being random and appearing to be close enough to be random?

Matthew Brown
Bartender
Posts: 4568
9
Akhilesh Trivedi wrote:
What is not being random and appearing to be close enough to be random?

It's not random because it's entirely predictable - it's the result of an algorithm. Therefore, by definition, it isn't random.

It's close enough to random because it's very difficult to tell the difference between the output of a good pseudo-random generating algorithm and true random numbers. So unless you have a requirement for the numbers to be truly random then pseudo-random numbers are good enough. In most cases, apparent randomness is fine.

Use the Java Random class to print out, let's say 100 numbers. The output looks pretty random, doesn't it? You'll see some localised patterns, but then true random numbers have those as well. You need some pretty powerful mathematical tools to tell the difference.