The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » sci.math.* » sci.math

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to make weighted random numbers?
Replies: 7   Last Post: Feb 28, 2009 3:49 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 1,677
Registered: 12/1/07
Re: How to make weighted random numbers?
Posted: Feb 28, 2009 2:45 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Feb 27, 6:36 pm, Rich Grise <> 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
> quick-n-dirty 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 bell-shaped 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 3-d 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 uniformly-distributed 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) = (1-cos(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., or
, slides 25-27). 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(1-2*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 cube-root 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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.