# Simulation random points on a circle

**URL:** <https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363>\
**Category:** General Usage\
**Created:** [March 8, 2024, 3:14pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363 "2024-03-08T15:14:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francesco\_C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francesco_c/32/51943_2.png) [@Francesco\_C](https://discourse.julialang.org/u/Francesco_C)\
**Post date:** [March 8, 2024, 3:14pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/1 "2024-03-08T15:14:02Z")

</div>

Hi,

I am currently working on simulating random points on a circle based on a non-uniform distribution. Initially, I successfully generated these random points using a set of code lines. However, some of the simulated random points fell outside the circle when wrapping these lines within a function and calling it. Despite ensuring the correctness of the input variables for the function, my guess is that when the code executes within the function, the vector containing the probabilities associated with specific polar coordinates becomes disordered, leading to points being generated outside the circle.

What could be the issue? Sorry but the code is too long to be pasted here.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 8, 2024, 3:32pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/2 "2024-03-08T15:32:13Z")

</div>

If the code is too long to share, please share an example of an input/output pair (or more than one). There is a good chance this will be enough to generate some working code, which might be even shorter.

---

<div class="post-metadata">

**Author:** ![Francesco\_C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francesco_c/32/51943_2.png) [@Francesco\_C](https://discourse.julialang.org/u/Francesco_C)\
**Post date:** [March 8, 2024, 3:56pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/3 "2024-03-08T15:56:09Z")

</div>

Here is a minimal code with comments

> #struct containing the domain  
> circ = Circle(0., 0., R)
> 
> #generate random number  
> Np = rand(Poisson(Npar))
> 
> #create a matrix containing the cartesian position of the points  
> X = Array{Float64}(undef, 0, Nd);
> 
> function simulate\_model(particle::Particle, circ::Circle, Np::Int64, Rk::Float64, D::Float64, X::Matrix{Float64})
> 
> ```
> for i in 1:Np
> #this first block returns the correct results
> x, y = GenerateHit(circ, Rk);
> track = Track(x, y, Rk)
> #integral contains the probability of generate a point at radius ρ, theta the porbability of generating a point at angle θ along radius ρ
> #Dd is a variable needed for other purposes (but it is calculated correctly)
> #radius is a vector of possible radii
> integral, theta, Dd, radius = distribute_D(particle, circ, track);
> 
> #next line returns points outside the circle
> X_, Y_ = calculate_position(particle, circ, integral, theta, Dd, radius);
> 
> #I check if the position is within a radius circ.r, if outside is wrong
> dist = sqrt.(X_[:,1].*X_[:,1] .+ X_[:,2].*X_[:,2])
> if size(dist[dist .> circ.r],1) != 0
> println("Error")
> return X_
> end
> 
> #I concatenate the matrix of positions
> X = vcat(X,X_)
> D += Dd
> end
> 
> return X
> 
> ```
> 
> end

The problem is the function _calculate\_position_ that gives different answers, whether executed inside the function or as a single line of code. I noted that changing variables from global to local will give different results without converging into a stable solution.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 8, 2024, 4:05pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/4 "2024-03-08T16:05:10Z")

</div>

I don’t think much can be learned from the code you posted as it depends on a lot of things not included.

Are you saying that it is the call to

```julia
calculate_position(particle, circ, integral, theta, Dd, radius)

```

that gives you a problem? Then I would maybe amend your `if` block to:

```julia
if size(dist[dist .> circ.r],1) != 0
            println("Error")
            return (; X_, particle, circ, integral, thea, Dd, radius)
        end

```

so you can inspect which input isn’t what you expected?

---

<div class="post-metadata">

**Author:** ![Francesco\_C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francesco_c/32/51943_2.png) [@Francesco\_C](https://discourse.julialang.org/u/Francesco_C)\
**Post date:** [March 8, 2024, 4:16pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/5 "2024-03-08T16:16:20Z")

</div>

The input seems correct. I am mostly wondering how it is possible that if I run the function I get correct positions but if I run a function that calls _calculate\_position_ I get wrong positions.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 8, 2024, 4:38pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/6 "2024-03-08T16:38:26Z")

</div>

> [@Francesco\_C](#):
>
> `size(dist[dist .> circ.r],1) != 0`

This thing in Julia can probably be written as (not tested):

```julia
any(>(circ.r), dist)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 8, 2024, 8:16pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/7 "2024-03-08T20:16:01Z")

</div>

To quote code enter three backquotes (`) on a separate line, to start code block, and end with another line with three backquotes.

It would be much easier to work with the code for the problematic `calculate_position` function, or at the minimum the parameters causing the output to go outside circle.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 8, 2024, 9:25pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/8 "2024-03-08T21:25:23Z")

</div>

How are you doing your conversion from polar to cartesian coordinates and how are you sampling your \theta values? My guess is that you’re either sampling \theta outside of the [0,2\pi] range, or there’s a problem when converting to cartesian coordinates. I just played around with the example below and it seems to work fine:

```julia
using Distributions
using StatsPlots

function circle_shape(h, k, r)
	θ = LinRange(0, 2 * π, 500)
	h .+ r * sin.(θ), k .+ r * cos.(θ)
end

r = 10
circ = circle_shape(0,0,r)
d = truncated(Normal(5), 0, 2π)
θ = rand(d, 10)
x = r .* cos.(θ)
y = r .* sin.(θ)
plot(circ, aspect_ratio=1, legend=false)
scatter!(x,y)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa6d0771f6564316fae5359d97ed07bf63600c99.png)

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [March 8, 2024, 11:29pm UTC](https://discourse.julialang.org/t/simulation-random-points-on-a-circle/111363/9 "2024-03-08T23:29:09Z")

</div>

It sounds like the issue you’re seeing is particular to the function definition that you haven’t shared, so I’m not sure there’s much we can do to help you debug that blindly.

I will add, though, that you might be interested in Meshes.jl which has a lot of built-in tooling for geometric definitions, parametric functions for generating points, and even random sampling tools. Here’s a quick example of

```julia
using Distributions
using Meshes

# Create a 2D surface bounded by a unit circle
center = Meshes.Point(0, 0)
radius = 1.0
circle = Meshes.Ball(center, radius)

# Some distributions from Distributions.jl with supports in [0,1]
dist_r = Normal(0.5, 0.1)
dist_phi = Normal(0.25, 0.1) # centered on pi/2

# Get a collection of N sample Meshes.Point's on the surface of the circle
# Using your geometric variable name as a function turns it into a 
# parametric function, e.g. circle(r, phi) -> the Point at that location
N = 10
sample_rs = rand(dist_r, N)
sample_phis = rand(dist_phis, N)
samples = circle.(sample_rs, sample_phis)

```
