如何计算 Julia 中的散点

How to count scattered points in Julia

我想数一下圆圈内分散的红点。
我的代码是:

using PyPlot # Here I define the circle

    k = 100
    ϕ = range(0,stop=2*π,length=k)
    c =  cos.(ϕ)
    d =  sin.(ϕ)

# Here I defined the scattered points with the circle

function scatterpoints(x,y)
    n = 1000
    x = -n:n
    x = x /  n
    y = rand(2*n+1)

    scatter(-x, -y;c="red",s=1)
    scatter(x, y;c="red", s=1)
    plot(c,d)
end

scatterpoints(x,y)

我的方法(伪代码)是这样的:

using LinearAlgebra
if norm < radius of circle then
amount of points in circle = amount of points in circle + 1
end

不幸的是,我不确定如何在 Julia 中实现它。

你的伪代码已经差不多了

using LinearAlgebra

n = 1000
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(n_in_circle) # 1565
println(pi*N/4)      # 1571.581724958294