Jython 随机模块产生与 cpython 不同的结果
我正在使用已知的随机种子生成一些测试数据。我想使用来自 cpython 和 jython 的数据。我发现如果我使用 jython (2.5.2) 与 cpython,数据会有所不同。
将其归结为一个简单的测试,我可以看到 PRNG 在两种实现中给出了不同的结果:
在 Jython 中:
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Server VM (Sun Microsystems Inc.)] on java1.6.0_26
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(1)
>>> random.random()
0.7308781974052877
在 CPython 中:
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(1)
>>> random.random()
0.13436424411240122
我生成的测试数据在每个 python 实现中都是可重现的。有办法解决这个问题吗?也许我需要编写自己的 PRNG 代码?
I'm generating some test data using a known random seed. I want to use this data from cpython and from jython. I've found that the data is different if I use jython (2.5.2) vs cpython.
Boiling it down to a simple test, I can see that the PRNG is giving different results in the two implementations:
In Jython:
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Server VM (Sun Microsystems Inc.)] on java1.6.0_26
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(1)
>>> random.random()
0.7308781974052877
In CPython:
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(1)
>>> random.random()
0.13436424411240122
The test data I'm generating is reproducible within each python implementation. Is there a way around this? Maybe I need to code my own PRNG?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种方法可以解决这个问题。两种实现都包含纯 python“WichmannHill”PRNG。
它速度较慢,但在 Jython 和 CPython 中给出相同的结果。
在我的代码中我替换
为
There is a way around this. Both implementations include the pure-python "WichmannHill" PRNG.
It's slower but it gives the same results in both Jython and CPython.
In my code I replaced
with
正如 delnan 在评论中所说:不同的 python 解释器生成不同的随机序列并不奇怪。 官方文档指的是算法的 C 实现。其他 Python 实现可能会选择其他算法。事实上,最小公分母可能是生成的随机序列的分布。
如果您依赖可以在所有 Python 解释器中重现的伪随机序列,则必须编写自己的伪随机数生成器。 线性反馈移位寄存器可能是一个好的开始并且相对容易理解。
As said by delnan in a comment: It is not a surprise that different python interpreters generate different random sequences. The official documentation refers to the C implementation of an algorithm. Other Python implementations may choose other algorithms. In fact, the lowest common denominator might be the distribution of the produced random sequences.
If you depend on pseudo-random sequences which can be reproduced across all Python interpreters you have to write your own pseudo-random number generator. A linear feedback shift register may be a good start and relatively easy to understand.