Do's and Don'ts in Random Star GenerationThis post could well be viewed as a mini-tutorial about some basics in randomness.
Since the random generation of stars or other celestial objects plays a very important role in
celestia.Sci, I decided to present here some elementary exercises before turning to more advanced stochastic methods in a subsequent post.
Note: Whenever one talks about picking numbers x randomly, one has to specify the corresponding
probability density function p(x)!
The simplest one is just a constant and is called a
uniform distribution. Every programming language offers a random command that refers to a uniform probability distribution. In C++ for example, Mathf::frand() and Mathf::sfrand() generate float random numbers in [0,1) and (-1,1), respectively.
You all know the discrete version of the uniform distribution from playing dice
. For one die the probability of throwing a number between 1 and 6 is constant and equals 1/6 for each number, such that the sum adds up to 1.
A) Suppose you were given the task of picking stars uniformly distributed on the surface of a sphere with radius 1. How would you do it?
Sounds almost trivial, right? You parametrize the 3-vector of each point on the unit sphere by 2 angular polar coordinates θ and Φ (with radius set r = 1), in analogy to latitude and longitude, like so
Then you pick each one of your 2 angular coordinates randomly from uniform distributions
θ in [0,2π) and Φ in [0,π].
Unfortunately this is a
Don't. To convince you, I repeated the described procedure in Maple. Here is how your result would look like:
Attachment:
unitsphere_wrong.jpg [ 64.65 KiB | Viewed 11754 times ]
You clearly see the
accumulation of stars near the north and south poles, which is NOT what you set out to accomplish!
So what went wrong??
To get this right, you have to first consider the respective differential element expressed in the chosen coordinates that appears in the normalization integral of your probability distribution. In the above example of the unit sphere surface, we consider the differential area element (solid angle) which should be familiar:
dΏ = sinΦ dΦ dθ = - d(
cosΦ) dθ
and thus the relevant probability density p(Ώ) satisfies
p(Ώ) dΏ =
constant d(
cosΦ) dθ
with these variables!
++++++++++++++++++++++++++++++++++
Apparently for this task, a constant uniform distribution requires cosΦ NOT Φ to be picked randomly along with θ
++++++++++++++++++++++++++++++++++
Again, I did it for you in Maple. Calling now
cosΦ = u, we rewrite our vector as follows
and
randomly pick u and θ from our uniform distribution. Here is the result:
BINGO!Attachment:
unitsphere_right.jpg [ 54.37 KiB | Viewed 11753 times ]
This looks pretty cool doesn't it?
Next consider a second task that now involves 3D instead of 2D.
B) You now are to pick stars uniformly distributed within a 3D sphere of radius 1.This partly resembles the previous problem, whence we already know how to pick the 2 angular coordinates correctly for stars on the surface of any sphere of radius r <=1. The new element is to
correctly pick the 3rd coordinate associated with the
radius from a uniform distribution!
Firstly, here is the
Don't method
This method will now be bypassed hopefully by all of you
It consists in just picking r in [0,1) randomly from the uniform distribution besides the 2 angular coordinates
cosΦ = u and θ.
Unfortunately the result again doesn't look like a uniform distribution of stars within the spherical volume:
Attachment:
sphere_wrong.jpg [ 48.71 KiB | Viewed 11754 times ]
Clearly there are far too many stars at small values of the radial coordinate!
How to get this right?
Again, consider the appropriate differential measure which is now the differential
volume element dV in 3D polar coordinates. Since we all know the volume of a sphere by heart,
we rewrite the familiar result (with θ and Φ already integrated over => 4π )
The solution is --analogously to the above recipe-- to pick the volume V randomly from the uniform distribution and solve for r, which apart from a constant factor amounts to
Here is the result:
BINGO!Attachment:
sphere_right.jpg [ 83.43 KiB | Viewed 11754 times ]
After these basic exercises, I'll report very soon about the actual, more advanced statistical methods I used to render the
globular cluster stars and the
galactic halo stars etc in celestia.Sci.
Let me know whether this was anywhere instructive to some of you?
Fridger