“随机”的独立实例
下面的代码试图说明我想要的。我基本上想要两个彼此独立运行的“随机”实例。我想在一个类中播种“随机”,而不影响另一个类中的“随机”。我怎样才能做到这一点?
class RandomSeeded:
def __init__(self, seed):
import random as r1
self.random = r1
self.random.seed(seed)
def get(self):
print self.random.choice([4,5,6,7,8,9,2,3,4,5,6,7,])
class Random:
def __init__(self):
import random as r2
self.random = r2
self.random.seed()
def get(self):
print self.random.choice([4,5,6,7,8,9,2,3,4,5,6,7,])
if __name__ == '__main__':
t = RandomSeeded('asdf')
t.get() # random is seeded within t
s = Random()
s.get()
t.get() # random should still be seeded within t, but is no longer
The below code attempts to illustrate what I want. I basically want two instances of "random" that operate independently of each other. I want to seed "random" within one class without affecting "random" in another class. How can I do that?
class RandomSeeded:
def __init__(self, seed):
import random as r1
self.random = r1
self.random.seed(seed)
def get(self):
print self.random.choice([4,5,6,7,8,9,2,3,4,5,6,7,])
class Random:
def __init__(self):
import random as r2
self.random = r2
self.random.seed()
def get(self):
print self.random.choice([4,5,6,7,8,9,2,3,4,5,6,7,])
if __name__ == '__main__':
t = RandomSeeded('asdf')
t.get() # random is seeded within t
s = Random()
s.get()
t.get() # random should still be seeded within t, but is no longer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
random.Random 类的存在是专门为了允许您想要的行为——模块本质上是单例,但类应该被多重实例化,因此两种需求都得到满足。
如果您需要一个模块的独立副本(在
随机
的情况下您绝对不需要!),请尝试在其上使用copy.deepcopy
- 在许多情况下情况下它会起作用。然而,这种需求非常罕见,因为模块通常不会保留全局可变状态,除非保留它们也提供用于“外部消耗”的类的一个特权实例(除了随机
之外的其他示例包括文件输入
)。Class
random.Random
exists specifically to allow the behavior you want -- modules are intrinsically singletons, but classes are meant to be multiply instantiated, so both kinds of needs are covered.Should you ever need an independent copy of a module (which you definitely don't in the case of
random
!), try usingcopy.deepcopy
on it -- in many cases it will work. However, the need is very rare, because modules don't normally keep global mutable states except by keeping one privileged instance of a class they also offer for "outside consumption" (other examples besidedrandom
includefileinput
).对于种子随机数,创建您自己的
random.Random
实例。random
文档 解释了模块所依赖的此类当您直接使用其中的函数时的单个实例。For the seeded random numbers, make your own instance of
random.Random
. Therandom
documentation explains this class, which the module depends on a single instance of when you use the functions directly within it.遗憾的是,拥有两个独立的 RNG 的随机性可能不如使用生成序列中的“偏移”的单个 RNG 的随机性。
使用“偏移”意味着您必须生成两个完整的样本序列,然后将它们用于模拟。像这样的东西。
RNG 只能被证明对于单个种子和单个数字序列具有理想的随机性属性。由于两个并行序列对乘数和模数使用相同的常数,因此它们有可能彼此具有可检测的相关性。
Sadly, having two independent RNG's is can be less random than having a single RNG using an "offset" into the generated sequence.
Using an "offset" means you have to generate both complete sequences of samples, and then use them for your simulation. Something like this.
RNG's can only be proven to have desirable randomness properties for a single seed and a single sequence of numbers. Because two parallel sequences use the same constants for the multiplier and modulus, there's a chance that they can have a detectable correlation with each other.