简体   繁体   English

JavaScript Math.Random()连续两次创建相同数字的几率是多少?

[英]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 使用 - http://en.wikipedia.org/wiki/Binomial_probability

Looks like values are from .0000000000000000 to .9999999999999999 看起来值从.0000000000000000到.9999999999999999

Probability of happening twice = p^2 = (1/9999999999999999)^2 = 1.0 e-32 发生两次的概率= 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. 我认为连续获得两个数字的概率是1除以生成器的范围,假设它具有良好的分布。

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; 连续两次获得某个特定数字,例如连续两次0.5,将是p ^ 2; however, if you just care about any number twice in a row, it's just p. 但是,如果你只是连续两次关心任何数字,那只是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). 在理想世界中,Math.random()绝对是随机的,其中一个输出完全独立于另一个输出,(假设p =产生任何给定数字的概率)导致重复的任何值可能为p ^ 2紧接着(正如其他人已经说过的那样)。

In practice people want Math.random to be fast which means pseudo-random number generators are used by the engines. 在实践中,人们希望Math.random快速,这意味着引擎使用伪随机数生成器。 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: 有许多不同类型的PRNG,但最基本的是线性同余发生器,它基本上是一个功能:

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. 如果使用这样的生成器,那么在调用random() some_other_prime次之前,您将看不到重复的值。 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. 然而,相对最近,显而易见的是,这种行为(加上PRNG与当前时间一起播种)可以用于某些形式的跟踪导致浏览器做了很多事情,这意味着你不能假设随后的random()电话。

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. 如果这些数字是真正随机的,那么你会发现它们确实以1 / p的概率出现,所以两倍就是1 / p ^ 2。

The value for p is not exactly the one you have though, because the numbers are being represented internally as binary. p的值不完全是你所拥有的值,因为数字在内部表示为二进制。 Figure out how many bits of mantissa the numbers have in javascript and use that for your combinatoric count. 计算出数字在javascript中有多少位尾数,并将其用于组合计数。

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. Knuth在Seminumerical Algorithms中做了一些可爱的工作,但基本上大多数常见的PN发生器至少有一些光谱分布。 Cryptograp0hic PN generators are generally stronger. Cryptograp0hic PN发生器通常更强大。

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. 得到2个给定数字的概率是(1 / p)^ 2,但得到2个相同数字(任意)的概率是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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