Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!dsinc!netnews.upenn.edu!truth.hep.upenn.edu!efrank From: efrank@truth.hep.upenn.edu (Ed Frank) Newsgroups: comp.lang.fortran Subject: Re: Random Number Functions--Generate versus Input Summary: generation of direction cosines Keywords: generation of direction cosines Message-ID: <32565@netnews.upenn.edu> Date: 9 Nov 90 15:54:06 GMT Expires: 15 Dec 90 05:00:00 GMT References: <1990Nov8.170334.24579@athena.mit.edu> <1990Nov8.215752.7075@athena.mit.edu> Sender: news@netnews.upenn.edu Reply-To: efrank@truth.hep.upenn.edu (Ed Frank) Followup-To: comp.lang.fortran Distribution: na Organization: University of Pennsylvania Lines: 42 In article <1990Nov8.215752.7075@athena.mit.edu> oliver@athena.mit.edu (James D. Oliver III) writes: >I guess I need to make this clear: the random number genertor itself is >vectorizable, however, the values of interest to me are *functions* of >these randome variables. 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. However, the fact that this >method is not vectorizable is just of incidental interest, since it remains >the fastest way of doing it. >____________________________ > Jim Oliver > oliver@athena.mit.edu / joliver@hstbme.mit.edu > oliver%mitwccf.BITNET@MITVMA.MIT.EDU First, it is not clear to me that this is the fastest method. My method of generating random direction cosines is as follows. Let DIR() be the array of direction cosines, and let RANN() be the (uniform) random number generator. PI is 3.14. Then Phi = 2.0 * PI * RANN( dum) ! in polar coord. Phi and cos(theta) Dir(3) = -1.0 + 2.0*RANN( dum) ! follow a flat distribution. Sth = SQRT( 1.0 = Dir(3)**2) ! convert cos(theta) to sin(theta) Dir(1) = Sth * COS( phi) ! now get X and y Dir(2) = Sth * SIN( phi) ! direction cosines will produce the random directions for you. You might want to include this in your benchmark. I think that this will beat your method with respect to speed. Second, I do not completely understand your method. If I am reading your description correctly, you will *NOT* obtain a uniform distribution with your technique. Have you histogrammed the direction cosines produced by your method? The distributions should be flat. Keep in mind that if you distribute points on the surface of a sphere uniformly, and then project that distribution onto a plane, you will not obtain a flat distrubution bounded by x**2+y**2 = 1. Most of the sphere's projected area will be near x**2 + y**2 = 1. In short: Phi is flat from 0 to 2*pi Cos(theta) is flat from -1 to 1 Ed Frank efrank@upenn5.hep.upenn.edu