“随机”的独立实例

发布于 2024-08-20 09:58:39 字数 739 浏览 4 评论 0原文

下面的代码试图说明我想要的。我基本上想要两个彼此独立运行的“随机”实例。我想在一个类中播种“随机”,而不影响另一个类中的“随机”。我怎样才能做到这一点?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-08-27 09:58:39

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 using copy.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 besided random include fileinput).

幸福还没到 2024-08-27 09:58:39

对于种子随机数,创建您自己的 random.Random 实例。 random 文档 解释了模块所依赖的此类当您直接使用其中的函数时的单个实例。

For the seeded random numbers, make your own instance of random.Random. The random documentation explains this class, which the module depends on a single instance of when you use the functions directly within it.

ゝ偶尔ゞ 2024-08-27 09:58:39

遗憾的是,拥有两个独立的 RNG 的随机性可能不如使用生成序列中的“偏移”的单个 RNG 的随机性。

使用“偏移”意味着您必须生成两个完整的样本序列,然后将它们用于模拟。像这样的东西。

def makeSequences( sequences=2, size=1000000 ):
    g = random.Random()
    return [ [ g.random() for g in xrange(size) ] for s in xrange(sequences) ] ]

t, s = makeSequences( 2 )

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.

def makeSequences( sequences=2, size=1000000 ):
    g = random.Random()
    return [ [ g.random() for g in xrange(size) ] for s in xrange(sequences) ] ]

t, s = makeSequences( 2 )

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文