简体   繁体   中英

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

Is this correct? using - http://en.wikipedia.org/wiki/Binomial_probability

Looks like values are from .0000000000000000 to .9999999999999999

Probability of happening twice = p^2 = (1/9999999999999999)^2 = 1.0 e-32

I think I am missing something here?

Also, how does being a pseudo random number generator change this calculation?

Thank You.

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

The reason for this is that the first number can be anything , and the second number needs to just be that number again, which means we don't 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 any particular number once.

Getting some particular number twice in a row, eg two 0.5s in a row, would be p^2; however, if you just care about any number twice in a row, it's just p.

In an ideal world Math.random() would be absolutely random, with one output being completely independent from another, which (assuming p=the probability of any given number being produced) results in a probably of p^2 for any value being repeated immediately after another (as others have already said).

In practice people want Math.random to be fast which means pseudo-random number generators are used by the engines. There are many different kinds of PRNG but the most basic is a linear congruential generator, which is basically a function along the lines of:

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

If such a generator is used then you won't see a value repeated until you've called random() some_other_prime times. You're guaranteed of that.

Relatively recently however it's become apparent that this kind of behaviour (coupled with seeding the PRNGs with the current time) could be used for some forms tracking have led to browsers doing a number of things that mean you can't assume anything about subsequent random() calls.

If the numbers were truly random, you'd expect them, indeed, to appear with probability 1/p, so twice that would be 1/p^2.

The value for p is not exactly the one you have though, because the numbers are being represented internally as binary. Figure out how many bits of mantissa the numbers have in javascript and use that for your combinatoric count.

The "pseudorandom" part is more interesting, because the properties of pseudorandom number generators vary. Knuth does some lovely work with that in Seminumerical Algorithms , but basically most usual PN generators have at least some spectral distributiuon. Cryptograp0hic PN generators are generally stronger.

Update : The amount of time shouldn't be significant. Whether it's a millisecond or a year, as long as you don't update the state The probabilities will stay the same.

The probability that you would get 2 given numbers is (1/p)^2, but the probability that you get 2 of same numbers (any) is 1/p. That is because the first number can be anything, and the second just needs to match that.

You can kind of find out, just let it run 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM