Path: utzoo!attcan!uunet!convex!convex.COM From: dodson@convex.COM (Dave Dodson) Newsgroups: comp.lang.fortran Subject: Re: Random Number Functions--Generate versus Input Message-ID: <108474@convex.convex.com> Date: 9 Nov 90 18:11:42 GMT References: <1990Nov8.170334.24579@athena.mit.edu> <1990Nov8.215752.7075@athena.mit.edu> Sender: usenet@convex.com Reply-To: dodson@convex.COM (Dave Dodson) Organization: Convex Computer Corporation; Richardson, TX Lines: 42 In article <1990Nov8.215752.7075@athena.mit.edu> oliver@athena.mit.edu (James D. Oliver III) writes: >To be more specific, I'm generating a series of >randomly oriented 3-D unit vectors. The fastest way to do this is to >generate two random numbers for x and y and calculate z as long as x**2 + >y**2 <= 1. This process, however, involves a conditional loop back to the >ranf() function and thus is not vectorizable. I'd generate an array of candidates and filter out the inadmissable ones. This could all be vectorized with a SCILIB routine as follows: 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 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. It returns nindx random 3-d unit vectors in (x(i),y(i),z(i),i=1,nindx). 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. Alternatively, generate x, y, and z from a Normal distribution and just normalize to unit length, but, of course, it is much harder to get Normal numbers than uniform. ---------------------------------------------------------------------- Dave Dodson dodson@convex.COM Convex Computer Corporation Richardson, Texas (214) 497-4234