What are the chances that JavaScript Math.Random () will create the same number twice in a row?

It is right? using - http://en.wikipedia.org/wiki/Binomial_probability

It looks like values ​​are from .0000000000000000 to .999999999999999999

Repeat probability twice = p ^ 2 = (1/9999999999999999) ^ 2 = 1.0 e-32

I think something is missing here?

Also, how does changing a pseudo-random number change this calculation?

Thanks.

+7
source share
5 answers

In an ideal world, Math.random () will be completely random, and one output will be completely independent of the other, which (provided that p = the probability of any given number) leads to the probability p ^ 2 for any value is repeated immediately after the other (as already others said).

In practice, people want Math.random to be fast, which means that pseudo random number generators are used by engines. There are many different types of PRNG, but the simplest is a linear congruent generator, which is basically a line-by-line function:

s(n + 1) = some_prime * s(n) + some_value mod some_other_prime 

If such a generator is used, you will not see the value repeating until you name random() some_other_prime times. You are guaranteed by this.

More recently, however, it has become apparent that this behavior (combined with sowing PRNG with the current time) can be used to track some forms, which has led browsers to do a number of things, meaning that you cannot accept anything from subsequent calls random() .

+4
source

I think the probability of getting two numbers in a line is 1 divided by the range of the generator, assuming it has a good distribution.

The reason for this is that the first number can be any, and the second number should just be that number, which means that we do not care about the first number at all. The probability of getting the same number twice in a row is the same as the probability of getting a certain number once.

Getting a specific number twice in a row, for example. two 0.5 s in a row, there will be p ^ 2; however, if you just care about any quantity twice in a row, it's just p.

+5
source

If the numbers were really random, you would expect them to really appear with a probability of 1 / p, so twice this would be 1 / p ^ 2.

The value for p is not what you have, because the numbers are represented internally as binary. Find out how many mantissa bits are in javascript and use this for combinatorial counting.

The "pseudo-random" part is more interesting because the properties of pseudo-random number generators are changing. Knut does a great job of this in Seminumerical Algorithms, but basically most ordinary PN generators have at least some spectral distribution. PN cryptographic generators are generally stronger.

Update: time should not be significant. Be it a millisecond or a year if you are not updating the state. The probabilities will remain unchanged.

+2
source

The probability that you will get 2 data is (1 / p) ^ 2, but the probability that you will get 2 identical numbers (any) is 1 / p. This is because the first number can be anything, and the second just needs to match that.

+2
source

You can find out just let it run for a few days :)

 var last = 0.1; var count = 0 | 0; function rand(){ ++count; var num = Math.random(); if(num === last){ console.log('count: '+count+' num: '+num); } last = num; } while(true) rand(); 
+1
source

All Articles