PDA

View Full Version : Math.random is it random?


scroots
01-22-2003, 07:29 PM
if i have the following code

var randomnumber=Math.floor(Math.random()*11)

I was told that using a random number generator will not produce a proper random number as a sequence repeats itself therefore it is called a pseudorandom number.

Is this true?

scroots

Philip M
01-22-2003, 07:36 PM
Yes, you are quite right, but if the pseudo-random number generator is "efficient" it will take a huge number (billions or zillions) of numbers before repetition of the sequence occurs.

scroots
01-22-2003, 07:38 PM
thank you for your explanation, you see we have to do about picking a sample for maths and it said in a textbook about it and i wonderd if it was right and how long it takes to repeat.

thanks
scroots

ez4ne12c
01-22-2003, 09:15 PM
Just to mention

Most random number generators require a seed to generate the random number, this seed is just a number..

The seed is usually chosen as the current time on the system that is running the generator.

So the numbers arent actually cycled through and then repeated per say, because which number is generated depends on when it is generated, the cycle may never actually be repeated (depending on how it is used)


:thumbsup: ez

ps some implementations of random number generators allow you to manually set the seed, this can be useful when debugging and testing programs when you need a consistent result.. im not sure if js Math.random allows this
;)

beetle
01-22-2003, 09:26 PM
The random() method in JS does not allow for a seed.

However, as previously stated, it would take an exremely large number of repititions to create a true duplicate or reveal a pattern.

The random number generated by Math.random() is a decimal with 16 significant digits. It is rare in JS when more than 3 significant digits are needed, so the issue of a pattern emerging is moot.

scroots
01-22-2003, 09:35 PM
thank you to you all for you excellent information.
its amazing what you learn, when asking what i thought was a simple question.
scroots

ez4ne12c
01-22-2003, 09:48 PM
a little more info
http://www.dardenhome.com/js/x75863.htm

this author claims that some implementations do allow to set the seed manually but ive never seen how to do this in IE or NN..

All random number generators that i have seen require a seed but i think in most js implementations the seed is not accessable to the user. (after all you can just use a constant :eek: for testing)

ez:)