在 Julia 中使用 Sobol 序列计算圆周率

Using the Sobol-Sequence to calculate pi in Julia

可以通过查看内有圆的正方形上随机生成的点之间的关系来近似圆周率。

function picircle(n)
n = n
N = 2n+1

x = range(-1, 1, length=N)
y = rand(N)

center = (0,0)
radius = 1

n_in_circle = 0

for i in 1:N
    if norm((x[i], y[i]) .- center) < radius
        n_in_circle += 1
    end
end

println(4 * n_in_circle / N)
end
picircle(1000)
3.1424287856071964

不过,我想用准Monte Carlo的方法。我不想使用伪随机数,而是想使用 Sobol 序列中的数字。我知道如何生成它们,但我不确定如何在我的代码中实现它。

using Sobol
s = SobolSeq(2) # Creates a Sobol-Sequenz in 2 Dimensions

请参阅 Sobol.jlREADME.md 以了解如何迭代 SobolSeq。要点是我们可以使用 next!(s) 来获取接下来的 n 个元素,对于一个 n 维序列。

julia> using Sobol

julia> s = SobolSeq(2)
2-dimensional Sobol sequence on [0,1]^2

julia> N = 10_000_000
10000000

julia> 4 * count(hypot(next!(s)...) < 1 for _ in 1:N) / N
3.1415952