Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!yale!mintaka!bloom-beacon!athena.mit.edu!oliver From: oliver@athena.mit.edu (James D. Oliver III) Newsgroups: comp.lang.fortran Subject: Re: Random Number Functions--Generate versus Input Message-ID: <1990Nov13.053812.19914@athena.mit.edu> Date: 13 Nov 90 05:38:12 GMT References: <1990Nov8.170334.24579@athena.mit.edu> <1990Nov8.215752.7075@athena.mit.edu> <108474@convex.convex.com> Sender: daemon@athena.mit.edu (Mr Background) Reply-To: oliver@athena.mit.edu (James D. Oliver III) Organization: Massachusetts Institute of Technology Lines: 41 In article <108474@convex.convex.com>, dodson@convex.COM (Dave Dodson) writes: > real x(n),y(n),z(n),r(n) > integer indx(n) > do 10 i = 1, n > x(i) = 2.0 * ranf() - 1.0 > y(i) = 2.0 * ranf() - 1.0 > z(i) = 2.0 * ranf() - 1.0 > r(i) = x(i)**2 + y(i)**2 + z(i)**2 > 10 continue > call whenflt (n,r,1,1.0,indx,nindx) >cdir$ appropriate compiler directive to ignore dependencies in the loop. > do 20 i = 1, nindx > j = indx(i) > t = 1.0 / sqrt(r(j)) > x(i) = t * x(j) > y(i) = t * y(j) > z(i) = t * z(j) > 20 continue Your algorithm is the same as the original scheme proposed by von Neumann for generating random vectors. It's efficiency is given by the area of a unit cube divided by a unit sphere, so 18/pi=5.73 random numbers plus a square root must be generated for each vector, as opposed to the 2.55 plus a square root for the Marsaglia method I posted in a recent follow-up >You are using one of our competitors' machines, so I haven't tried this, >but I believe it will all vectorize on a Cray, as it does on a Convex. Hey, offer me a price, my code knows no loyalty! :-) >P.S. I don't think you can calculate z from x and y and get a truly uniform >distribution of unit vectors. You have to generate x, y, and z independently >and reject those outside the unit sphere to get uniformity. As I said, Marsaglia devised a clever way around this; see a more recent posting. -- ____________________________ Jim Oliver oliver@athena.mit.edu / joliver@hstbme.mit.edu oliver%mitwccf.BITNET@MITVMA.MIT.EDU