As pointed out earlier, in fact your question is of mathematics.
The origin of the problem
The problem is that you are taking a uniform distribution of rays, i.e., the probability of a point being at any distance r the centre does not depend on r. The intuitive way to see that this does not produce a uniform distribution in the area of the circle is as follows: if the probability of a point being at a distance r downtown is the same for any r, you expect to find on average the same number of points at a distance r downtown, for all r.
Moreover, as the probability is independent of the angle, the points at a distance r shall also be distributed over a circle of radius r. It turns out that to r large circles are getting bigger, and as you are distributing the same number of points on them, it is expected that the points are more spaced from each other!
For the distribution to be uniform in terms of area, you need to put more points on the larger circumferences, or in other words, the probability of putting a certain point at a distance r should grow with r. But grow how? Since the size of the circles grows linearly with the radius (=2πr), this probability is expected to grow also linearly with r. To see this more rigorously we need more mathematics.
Lightning distribution
It turns out that the area elements in spherical coordinates depend on the radius, getting larger to r bigger:
dA = r dr dθ,
and for the distribution to be in fact uniform, you want the probability of there being a point in a given element of area to be constant for any element. That probability is, by the definition of probability density, P(r,θ) of, that is to say, P(r,θ) r dr dθ.
Integrating this to r between 0 and R and θ between 0 and 2π you have the total probability of the point falling somewhere in the circle, which should be 1. It is not difficult to see that this fixed
P(r,θ) = 1 / (πR²),
what is obvious: in a homogeneous distribution over a given total area, the probability associated with an area element must be the ratio between the area of the element and the total area. See that this is exactly what gives the product P(r,θ) of:
P(r,θ) dA = r dr dθ / (πR²).
Inomogeneous variable
Well, so now you can interpret this result a little differently. Instead of the variables r and θ, we switched to two random variables x (ranging from 0 to R) and y (ranging from 0 to 2π) which now have a probability density
P(x,y) = x / (πR²)
so that the whole of it x going from 0 to R and y going from 0 to 2π be 1:
∫∫ P(x,y) dx dy = 1.
See that now your probability density P(x,y) in fact does not depend on y but is proportional to x! This is the result I mentioned at the beginning: actually you’re looking for a random variable x between 0 and R but whose distribution is not uniform, being in fact proportional to x.
To understand the square root in Rand(1,N) we still need to go a little further.
A small technicality: the variables x and y are independent as we can rewrite P(x,y) as a product
P(x,y) = f(x)g(y).
It’s easy to see that the proper choice is
g(y) = 1/2π
so that g(y) dy = 1 for the integral between 0 and 2π, and
f(x) = 2x/R²
such that f(x) dx = 1 for x between 0 and R. Note that f(x)g(y) actually recovers P(x,y).
Like g(y) does not depend on y, we see that the angle is actually distributed homogeneously (which explains why you can use 2*pi*Rand(1,N) within the exponential).
The challenge now is to find a variable x distributed according to probability density f(x) above.
Processing method
A method to do this from uniformly distributed variables is called processing method.
For variables x distributed from 0 to R according to a distribution f(x), the fraction of numbers falling between 0 and x, which we will call F(x), is given by
F(x) = ∫f(x') dx',
with the integral held between 0 and x.
It turns out that for numbers evenly distributed between 0 and 1, the fraction of numbers that fall between 0 and G (for a given G between 0 and 1) is exactly G. See: the fraction of numbers between 0 and 1/2 is 1/2, between 0 and 1/3 is 1/3, and so on.
Then the fraction of numbers between 0 and a given F(x) for numbers distributed evenly between 0 and 1 is F(x), which is exactly the fraction of numbers between 0 and x in the distribution f(x) between 0 and R.
So what we want to do is map the range between 0 and F(x) number r distributed homogeneously in the 0-x range of the new distribution. And that’s very easy to do: the end of the intervals must match! So the number x of non-uniform distribution shall be obtained when the
r = F(x)
is obtained in uniform distribution. Conclusion: given the numbers r distributed evenly, just reverse this relation - finding the inverse function of F(x) - to obtain the x. Let’s apply this idea to our f(x) above.
We have f(x) = 2x/R², so that remembering that the integral is held between 0 and x,
F(x) = ∫f(x') dx' = ∫2x'/R² dx' = x²/R².
Reversing that relationship is easy:
r = F(x) => r = x²/R² => x = R √r
what (finally!) shows that a variable x distributed between 0 and R with probability density f(x) = 2x/R² can be obtained by simply multiplying R by the square root of a variable r homogeneously distributed between 0 and 1. That’s exactly what you’re doing with
R * sqrt(rand(1,N))
in its second line of code. This 'empirical' solution of yours therefore really provides exactly homogeneously distributed numbers over a circle of radius R.
Your problem seems more mathematical to me than anything else. mathematics and Random and would put an image of your chart with the two distributions. In a look at the results, I perceive the difference and see the reason, after all
sqrt(0.2)~0.45
, but I couldn’t remember an efficient method of qualifying randomness.– Guto
Guys, the edition I made was not text correction, it was just the addition that Guto suggested, the tags, I did not understand why someone put the text in correction as if I had modified and made it difficult to understand if it is as it was before, kkkkk
– Hennan Lewis
I can understand why in the first case the points are more concentrated in the center. However I can’t explain why when taking out the square root they spread better, but I think they remain not uniform
– Jefferson Quesado
I don’t understand why you multiply by the exponential of a number in the factor of
j
, just like I didn’t see you putting the value of the theta angle;.– Jefferson Quesado
I know it’s been a long time, but since I haven’t opened the SOF for a long time, I decided to explain. One way to write an angle is to use an exponential with i2πX exponent, i being the value of the set of imaginaries such that sqrt(i)=-1. In the end, we just take the module out of the imaginary axis and calculate the tangent that we have magically an angle, it’s much simpler than it looks, and j is how Matlab represents the imaginary value.
– Hennan Lewis