How does a computer get a Random Number

Oct 25, 2006
11,036
11
91
This has been bugging me for some time now. How is it that in Java, using something like Math.random() will get you a random number?

I was under the impression that no algorithm could actually get a random number every time, unless it used an outside variable like the current second?
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
It doesn't. Run the function 2^32 times and you will see the sequence repeat.

You can have hardware designed to give randomer numbers.
 

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
I'm not sure if I know what I'm talking about here, but I believe the computer gets a seed (from the clock, I perhaps?), then uses that seed to generate the random number within the specified parameters that the program gives it.

It's true that machines can never be truly "random" unless you have some atoms in there decaying, and have the computer generate a number based off that... but that's going a little too far for this question. :p

Again, I don't remember where I heard this from, or how accurate it is, so bleh.
 

Auryg

Platinum Member
Dec 28, 2003
2,377
0
71
Originally posted by: DyslexicHobo
I'm not sure if I know what I'm talking about here, but I believe the computer gets a seed (from the clock, I perhaps?), then uses that seed to generate the random number within the specified parameters that the program gives it.

It's true that machines can never be truly "random" unless you have some atoms in there decaying, and have the computer generate a number based off that... but that's going a little too far for this question. :p

Again, I don't remember where I heard this from, or how accurate it is, so bleh.

I think you're right. I seem to remember using some random numbers in some programming languages and you could seed it with whatever you wanted - but the milliseconds on the clock was a good way to do it. Basically random to the end user.
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Lets talk about an example I know, the Rnd() function in VB. This creates a random number between 0 and 1. The 'seed' is taken from the clock initialy (the number of seconds since midnight to 2 decimal places) as long as the randomize() function is called, then the next call to rnd() uses the number generated previously as the new seed. If the randomize() function is not called, the rand() function will always return the exact name number every time the program is run.

The next number is mathmaticly generated to not repeat for 17 million some odd values. (hence pesudo random) the math is something like 'new number' = ('previous number' * 1,100,000,000 + 12,000,000) MOD (2^24). the 1.1billion and 12 million are not exact, they are more specific than what I have mut my memory isnt' that good (and it doesn't matter what number it is really) The 'previous number' has a default around 300,000 if the randomize() function is not called. This means the rand() function will return the same 'random' numbers in exactly the same order as it did last time the program was run.

The number is not 'random' at all. this is fine for lots of things, but not for others. If I need something that is closer to random there are other ways to go about it. Do not call the randomize statement every time or you will end up with a higher concentration of some numbers than others instead of something closer to a random distrobution.

<edit>
spelling
 

russell2002

Senior member
May 16, 2005
272
0
0
A random number is a number which can not be predicted. 2p...

Maths teachers seem to like the idea that no such thing exists as a random nunber.

I disagree.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: russell2002
A random number is a number which can not be predicted. 2p...

Maths teachers seem to like the idea that no such thing exists as a random nunber.

I disagree.

There were actually some long threads on this recently in this forum.

It is impossible to algorithmically generate a truly 'random' number, since the definition of the algorithm must determine the output. By seeding with a relatively random value (like the milliseconds value of the current time) and a good pseudorandom number generator (PRNG), you can get numbers that are generally very hard to predict.

But not impossible. There have been exploits based around systems that used time-of-day to seed their random number generators, for instance. There was an online poker room that got hacked like this a few years ago; obviously they didn't think that one through! http://www.cigital.com/news/index.php?pg=art&artid=20

Depending on what you think about quantum mechanics, there may actually be truly random events that simply cannot be predicted (or at least cannot be predicted to arbitrary degrees of accuracy). Things like radioactive decay (which QM says should be impossible to predict), or randomly sampled white noise or static are sometimes used to provide 'completely' random numbers -- although more frequently they are used to provide seeds to PRNG algorithms, since it is often hard to get a large number of random numbers quickly from such sources. With a totally unpredictable seed, a well-designed PRNG can be essentially impossible to predict as long as you reseed it frequently enough.
 

Gannon

Senior member
Jul 29, 2004
527
0
0
"It is impossible to algorithmically generate a truly 'random' number, since the definition of the algorithm must determine the output."

You don't have to use an algorithm you just have to sample a number from a non digital probability space. I believe you could generate random numbers from analog signals using a boolean probability field and then using the value as a truly random number.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: Gannon
"It is impossible to algorithmically generate a truly 'random' number, since the definition of the algorithm must determine the output."

You don't have to use an algorithm

I know you don't have to use an algorithm; I discussed that.

That doesn't change the fact that it is impossible to algorithmically (that is, through purely mathematical/logical operations on something resembling a Turing Machine architecture or subset thereof) generate a truly random number. Some sort of external seed or input is required to have even the most trivially 'random' behavior in such a system.

Note that in some other computing models, such as a quantum computer, this may not be true.

you just have to sample a number from a non digital probability space. I believe you could generate random numbers from analog signals using a boolean probability field and then using the value as a truly random number.

Did you read my post?

Things like radioactive decay (which QM says should be impossible to predict), or randomly sampled white noise or static are sometimes used to provide 'completely' random numbers -- although more frequently they are used to provide seeds to PRNG algorithms, since it is often hard to get a large number of random numbers quickly from such sources. With a totally unpredictable seed, a well-designed PRNG can be essentially impossible to predict as long as you reseed it frequently enough.