Advertisement

Random Point Within a Sphere

Started by May 19, 2002 06:31 PM
21 comments, last by Mulligan 22 years, 8 months ago
Whew... this took me a while.

It appears to be evenly distributed, but I suspect the "box method" will be faster.

  // random number from 0 to 1inline float random() {  return (float)rand() / (RAND_MAX + 1.0f);}// generate a random point within "radius" units of the originvoid random_point(float radius, float *x, float *y, float *z) {  float v1 = 2.0f * random() - 1.0f;  float v2 = sqrt(1.0f - v1*v1);  complex<float> v3(pow(complex<float>(v1, v2), 1.0f / 3.0f));  complex<float> v4(pow(complex<float>(v1, -v2), 1.0f / 3.0f));  complex<float> v5(-(v3 + v4 + complex<float>(0, sqrt(3.0f)) * (v3 - v4)) / 2.0f);  float height = v5.real();  float flat_mag = sqrt(random()) * radius * sqrt(1 - height * height);  float flat_angle = random() * 6.283185307179f;  *x = cos(flat_angle) * flat_mag;  *y = height * radius;  *z = sin(flat_angle) * flat_mag;}  
I''ve tested it, and the box method is ~7 times faster than the method I came up with. Oh well...
Advertisement
X,Y two random numbers in [0,1]

theta = 2*PI*X
phi = (1-sqrt(Y))*PI/2

then go with IndirectX''s solution

Will generate a uniform hemisphere.
Randomly pick +/-phi to get a sphere.

note: the sqrt is there to take into account the ''reduced surface'' at higher latitudes.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Arrr.

/me dies, I guess.
You need to generate a number between 0 and r^2 and use the square root of that number as your radius otherwise it is concentrated about the center of the sphere.
Keys to success: Ability, ambition and opportunity.
quote:
Original post by Mulligan
The reason I want them is because I''m making a galaxy simulator for a science project. Each star starts at a random point within the galaxy''s sphere of influence. As soon as I can get a good random point generator, I can refine it to fit to model true star distribution formulas. (if there are any). By the way, are there any for galaxy star placement like there are for atom electron placements?


There are many different galactic structures... some, like elliptic galaxies, have a nearly uniform distribution of stars while others, such as barred-spirals, have all stars contained in patern that looks like someone drops a piece of string across a whirlpool!

How detailed do you want to be in your simulator? Do you want to get something that very occasionally spits out something that looks and acts like a stable galaxy (not that galaxies are stable, but they look like it on human lifetime time scales!), or do you want something very realistic? I would suggest you go for the former... the latter requires a hell of a lot of computing power and a complex particle model.

The other problem you face is that the bright matter in galaxies accounts for only 10% of the necessary matter to explain their coherancy and their angular momentum. One way around this is to postulate 10 times as many stars as you want to see... and only make 10% of them visible!

Good luck,

Timkin
Advertisement
Should I start a new thread about the physics of Galaxies?
To continue a discussion of galaxies within this thread would have relevance to your original problem, but not your original query in this thread... so it would be somewhat off topic. Starting a new thread would also get other people involved that might not otherwise look at a thread on random number generation.

Before starting the thread though, I highly recommend you search the web... there is a ridiculous amount out there on astrophysics and cosmology. Also, this is a topic that local libraries love to buy books on, because it fascinates so many people.

After that, if you have particular questions, don''t hesitate to ask.

Cheers,

Timkin
Yea, I''ve been drowning myself in information about this type of thing. I''m only asking questions that I simply can''t find answers to. Thanks
I can't follow this whole thread, but..

Why not add three independent Gaussians, mean zero, standard deviation sigma, one for each coordinate axis? I think this gives a uniform distribution on the sphere, with radius Gaussian stdev \sqrt{3} * sigma.

Or something. Is that clear?

[Edit: No, it isn't uniform within the sphere (ball), but probably a Gaussian radial distribution is more what you want anyway.]

[edited by - greeneggs on May 26, 2002 5:53:15 PM]

This topic is closed to new replies.

Advertisement