gpt4 book ai didi

random - 并行随机数 julia

转载 作者:行者123 更新时间:2023-12-04 15:44:33 24 4
gpt4 key购买 nike

考虑生成 N 个随机数并将它们保存在数组中的基本迭代(假设我们对数组理解不感兴趣,并且我们不知道调用 rand (N))

function random_numbers(N::Int)
array = zeros(N)
for i in 1:N
array[i] = rand()
end
array
end

我对一个类似的函数感兴趣,它利用我的笔记本电脑的核心来生成相同的数组。我查过this nice blog其中引入了宏 @everywhere@spawn@parallel,但计算是“即时”进行的,并且不需要数组来保存数据。

我的印象是,这是非常基本的,可以使用函数 pmap 轻松完成,但我不熟悉并行计算。

我的目标是将此方法应用到我构建的函数中,以生成从异常分布中抽取的随机数。

最佳答案

我建议在并行进程中对随机数生成器进行更仔细的初始化,例如:

# choose the seed you want
@everywhere srand(1)
# replace 10 below by maximum process id in your case
@everywhere const LOCAL_R = randjump(Base.GLOBAL_RNG, 10)[myid()]
# here is an example usage
@everywhere f() = rand(LOCAL_R)

这样你:

  • 确保您的结果可重现;
  • 控制不同进程生成的随机序列之间不存在重叠。

关于random - 并行随机数 julia,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47468447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com