samedi 29 juillet 2017

Randomly distributed dots in a ball in C++

I tried to create 5000 uniformly distributed stars (that are represented by each dot) inside a ball of radius R=500, but is seems that the middle gets more points than the surface of the sphere, why ?

this is my attempt:

 R=500*rand()/double(RAND_MAX);
 theta=acos(-1+2*rand()/double(RAND_MAX));
 phi=2*3.14*rand()/double(RAND_MAX);
 x_pos=R*sin(theta)*cos(phi);
 y_pos=R*sin(theta)*sin(phi);
 z_pos=R*cos(theta);

and the result looks like this: enter image description here




Aucun commentaire:

Enregistrer un commentaire