Python多线程随机生成

2024-05-23 20:07:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在模拟中实现以下代码:

https://numpy.org/doc/stable/reference/random/multithreading.html

但我不能解决这个问题

如果我遵循链接中的示例,我会

mrng = MultithreadedRNG(10000000, seed=0)
mrng.fill()
print(mrng.values[-1])
> 0.0

所有其他值也都是0

如果我输入一个较小的数字,如40,我得到

mrng = MultithreadedRNG(40)
mrng.fill()
print(mrng.values[-1])
> array([1.08305179e-311, 1.08304781e-311, 1.36362118e-321,             nan,
       6.95195359e-310, ...., 7.27916164e-095, 3.81693953e+180])

我做错了什么?我只想将这个多处理代码实现到一个随机位(0/1)生成器中


Tags: 代码httpsorgnumpydochtmlrandomfill
1条回答
网友
1楼 · 发布于 2024-05-23 20:07:21

我相信示例中有一个bug。您必须将PCG64包装到生成器接口中

请尝试下面的代码

class MultithreadedRNG(object):
    def __init__(self, n, seed=None, threads=None):
        rg = PCG64(seed)
        if threads is None:
            threads = multiprocessing.cpu_count()
        self.threads = threads

        self._random_generators = [Generator(rg)]
        last_rg = rg
        for _ in range(0, threads-1):
            new_rg = last_rg.jumped()
            self._random_generators.append(Generator(new_rg))
            last_rg = new_rg

        self.n = n
        self.executor = concurrent.futures.ThreadPoolExecutor(threads)
        self.values = np.empty(n)
        self.step = np.ceil(n / threads).astype(np.int_)

    def fill(self):
        def _fill(gen, out, first, last):
            gen.standard_normal(out=out[first:last])

        futures = {}
        for i in range(self.threads):
            args = (_fill,
                    self._random_generators[i],
                    self.values,
                    i * self.step,
                    (i + 1) * self.step)
            futures[self.executor.submit(*args)] = i
        concurrent.futures.wait(futures)

    def __del__(self):
        self.executor.shutdown(False)

没有进行太多测试,但值看起来还可以

相关问题 更多 >