
Re: How to make weighted random numbers?
Posted:
Feb 28, 2009 2:45 AM


On Feb 27, 6:36 pm, Rich Grise <r...@example.net> wrote: > Say, I want to fill a sphere uniformly with random dots; the logical way > to do that in r,theta format; otherwise to make a sphere, you'd have to > do some heroic math on the three dimension (or is there some > quickndirty formula that can do it in one flop, that they don't tell > us high school (and Tech School, don't forget!) ? > > Either way. I don't want a uniform sphere  more like density through the > center looks like a bellshaped curve, i.e., denser in the middle? > > It's got to be computable, maybe with iterations, but that's what I > do best! I just have no idea what the algorithm would be. > > What I'm up to is I have a 3d drafting program and I want to approximate > a translucent ball effect. I can enter coordinates by copying the output > and paste it into the command line to automatically place n dots. ;) > > Thanks! > Rich
I don't quite understand your wanting a uniform distribution over the sphere but at the same time not wanting a uniform sphere. I'll assume (as in my first post) that you want a uniformlydistributed point within the sphere. You can use the rejection method I mentioned before, or you can also get such points directly, with no rejections (but with extra computations). For a uniformly distributed point in spherical polar coordinates x = r*sin(t)*cos(p), y = r*sin(t)*sin(p), z = r*cos(t), 0 <= r <= R, 0 <= t <= pi, 0 <= p <= 2*pi (t = theta, p = phi), the probability that the point falls into volume dV is dV/ [volume of sphere]= dV/(4/3 * pi *R^3). But dV = r^2*sin(t) *dr *dt *dp, so P{point in dV} = (3r^2/R^3 dr)*(1/2 *sin(t) dt) * dp/(2*pi)). This means that r, t and p are independent random variables with density functions f_r(r) = 3*r^2/R^3, f_t(t) = sin(t)/2 and f_p(p) = 1/ (2*pi). Therefore, the (cumulative) distributions are F_r(r) = r^3/R^3 (0 <= r <= R), F_t(t) = (1cos(t))/2 (0 <= t <= pi) and F_p(p) = p/ (2*pi), 0 <= p <= 2*pi. So, we can find the sample values of r, t and p by the cumulative inversion method (see, eg., http://en.wikipedia.org/wiki/Inverse_transform_sampling or www.csie.ntu.edu.tw/~cyy/courses/rendering/06fall/lectures/handouts/lec09_mc.ppt , slides 2527). Thus, if U1, U2 and U3 are three independent random numbers uniform from 0 to 1, the we can find r by solving F_r(r) = U1, can find t by solving F_t(t) = U2 and can find p by solving F_p(p) = U3. Therefore, r = R*U1^(1/3), t = arccos(12*U2) and p = 2*pi*U3. You don't need to waste any random numbers as in the rejection method, but you do need to compute a cuberoot and an 'arccos' function. Whether or not this method works better than rejection depends on how efficiently you can compute these functions as compared with generating three more random numbers. Of course, you then have to compute the point itself using x = r*sin(t)*cos(p), y = r*sin(t)*sin (p), z = r*cos(t); this can also be avoided in the rejection method.
R.G. Vickson

