gpt4 book ai didi

javascript - 预测 Javascript 的 Math.random 的种子

转载 作者:数据小太阳 更新时间:2023-10-29 04:02:02 24 4
gpt4 key购买 nike

好的,我正在研究如何使用 Math.random 方法生成随机数。到目前为止,我了解到它是从一个“随机”种子开始的,然后将该种子插入到一些复杂的方程式中以创建一个随机数。如果种子总是一样的,结果会不会总是一样?

听说 Math.random 的种子是通过当前时间生成的,对吗?他们必须一直使用当前时间,精确到毫秒或其他时间,因为如果你不这样做,你会得到相同的结果。

种子到底是什么?是诸如“10:45”之类的时间还是诸如“2012 年 11 月 8 日 10:45”之类的时间和日期,还是某种组合?

如何找到种子,以便预测输出?

我希望能够插入这个:

alert(Math.floor((Math.random()*10)+1));

进入我的网址栏,并能够预测结果。这可能吗?

最佳答案

我通过 Rhino 查看了 source code找出他们使用的伪随机函数。显然他们fall backJava standard library 中定义的 Math.random 函数.

Math.random 的文档说:

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

所以我检查了 java.util.Random 的文档并找到了 this (对于默认构造函数):

Creates a new random number generator. Its seed is initialized to a value based on the current time:

public Random() { this(System.currentTimeMillis()); }

Two Random objects created within the same millisecond will have the same sequence of random numbers.

所以现在我们可以肯定地知道种子是以毫秒为单位的当前时间。此外,second constructor 的文档说:

Creates a new random number generator using a single long seed:

public Random(long seed) { setSeed(seed); }

Used by method next to hold the state of the pseudorandom number generator.

documentation对于 setSeed 方法说:

Sets the seed of this random number generator using a single long seed. The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. The method setSeed is implemented by class Random as follows:

synchronized public void setSeed(long seed) {
this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
haveNextNextGaussian = false;
}

The implementation of setSeed by class Random happens to use only 48 bits of the given seed. In general, however, an overriding method may use all 64 bits of the long argument as a seed value. Note: Although the seed value is an AtomicLong, this method must still be synchronized to ensure correct semantics of haveNextNextGaussian.

actual method用于生成随机数的是nextDouble:

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

nextDouble函数的实现如下:

public double nextDouble() {
return (((long)next(26) << 27) + next(27))
/ (double)(1L << 53);
}

很明显吧dependsnext 函数上:

Generates the next pseudorandom number. Subclass should override this, as this is used by all other methods.

next函数的实现如下:

synchronized protected int next(int bits) {
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (int)(seed >>> (48 - bits));
}

这就是您要查找的伪随机函数。正如文档中所说:

This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

但是请注意,这只是 Rhino 使用的随机数生成器。 Spidermonkey 和 V8 等其他实现可能有自己的伪随机数生成器。

关于javascript - 预测 Javascript 的 Math.random 的种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com