简体   繁体   English

Python-从自定义时间生成随机数

[英]Python - Generate random number from custom time

So say that I generate a random number using random.random() on one system without a custom seed, but say that I wanted to also generate a random number ahead of time so that if I were to use random.random() at that time without seeding it would return the same number. 假设我在一个没有自定义种子的系统上使用random.random()生成了一个随机数,但是我想也要提前生成一个随机数,以便在那个时候使用random.random()没有种子的时间,它将返回相同的数字。 In essence, "predict" the random number. 本质上, “预测”随机数。 How might I customize the time that random uses to seed itself? 我该如何自定义随机种子播种的时间? I thought to do the following based on how the random module seeds: 我考虑根据随机模块的播种方式执行以下操作:

import random
import time

print random.random()
random.seed(long(time.time()*256))
print random.random()

However, that gave me separate numbers and I don't understand why. 但是,这给了我单独的数字,我不明白为什么。 Should they not be done at the same instance in time? 难道不应该在同一时间同时完成它们吗?

tl;dr TL;博士

How can I customize random.seed(x) where it would seed at a custom point in time. 我如何自定义random.seed(x) ,它将在自定义时间点播种。

Assume that random.random() is called a-new each time so it is the first number being generated, and not the second, third, fourth, etc. 假设random.random()每次都被称为new,因此它是生成的第一个数字,而不是第二,第三,第四等。

Note that I do not want to do this in the past, so not get the state and then restore it, but be able to generate what will be generated in the future. 请注意,我不想在过去执行此操作,因此不要获取状态然后将其还原,但是能够生成将来将要生成的内容。

CLARIFICATION: The system this will be used on does not have urandom implemented. 澄清:将使用的系统未实施urandom。

Actually I was wrong. 其实我错了。

It's possible to do it: Here's how: 可以这样做:方法如下:

import time
import random

def predicite_future(delay):
    random.seed(long(time.time() + delay) * 256)
    return random.random()

delay = 10

print "The next value will be:",predicite_future(delay)
print "Waiting for %s seconds"%delay
time.sleep(delay)
random.seed(long(time.time()) * 256)
print "As predicited, the value is:",random.random()

output: 输出:

The next value will be: 0.359211550742
Waiting for 10 seconds
As predicited, the value is: 0.359211550742

I think you actually do have a urandom of sorts or at the very least the following does not throw an exception 我想到你居然做有urandom各种各样的或至少是以下抛出异常

from binascii import hexlify as _hexlify
from os import urandom as _urandom
a = long(_hexlify(_urandom(16)), 16)

does it? 可以?

Now assuming that lets modify the random lib which can be found here . 现在假设让我们修改可以在这里找到的随机库。

first replace 首先替换

    if a is None:
        try:
            a = long(_hexlify(_urandom(16)), 16)
        except NotImplementedError:
            import time
            a = long(time.time() * 256) # use fractional seconds

with just 只是

    if a is None:
        import time
        a = long(time.time() * 256) # use fractional seconds
        print ("bottom - the seed is", a) # debug

Now in bottom of the file in the if __name__ == "__main__" remove the test and instead put 现在,在文件底部的if __name__ == "__main__"删除测试,然后放入

if __name__ == '__main__':
    from time import time
    then1 = long(time() * 256)
    from random import random, seed
    then2 = long(time() * 256)
    assert then1 == then2 # if fails here the time has changed

    print random() # unseeded
    seed(then1)
    print random() # use the seed with think it uses

Now assume the time we want to predict is d in the future then we could have then1 = long((time()+d) * 256) to predict a result of random.random() in the future. 现在假设我们要预测的时间是将来的d ,那么我们可以让then1 = long((time()+d) * 256)来预测将来的random.random()结果。

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

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