1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ///////////////////////////////////////////////////////////////////////////
36 #ifndef INCLUDED_IMATHRANDOM_H
37 #define INCLUDED_IMATHRANDOM_H
39 //-----------------------------------------------------------------------------
41 // Generators for uniformly distributed pseudo-random numbers and
42 // functions that use those generators to generate numbers with
43 // different distributions:
52 //-----------------------------------------------------------------------------
55 // Here is the copyright for the *rand48() functions implemented for
60 // Copyright (c) 1993 Martin Birgmeier
61 // All rights reserved.
63 // You may redistribute unmodified or modified versions of this source
64 // code provided that the above copyright notice and this and the
65 // following conditions are retained.
67 // This software is provided ``as is'', and comes with no warranties
68 // of any kind. I shall in no event be liable for anything that happens
69 // to anyone/anything when using this software.
78 //-----------------------------------------------
79 // Fast random-number generator that generates
80 // a uniformly distributed sequence with a period
82 //-----------------------------------------------
92 Rand32 (unsigned long int seed = 0);
95 //--------------------------------
96 // Re-initialize with a given seed
97 //--------------------------------
99 void init (unsigned long int seed);
102 //----------------------------------------------------------
103 // Get the next value in the sequence (range: [false, true])
104 //----------------------------------------------------------
109 //---------------------------------------------------------------
110 // Get the next value in the sequence (range: [0 ... 0xffffffff])
111 //---------------------------------------------------------------
113 unsigned long int nexti ();
116 //------------------------------------------------------
117 // Get the next value in the sequence (range: [0 ... 1[)
118 //------------------------------------------------------
123 //-------------------------------------------------------------------
124 // Get the next value in the sequence (range [rangeMin ... rangeMax[)
125 //-------------------------------------------------------------------
127 float nextf (float rangeMin, float rangeMax);
134 unsigned long int _state;
138 //--------------------------------------------------------
139 // Random-number generator based on the C Standard Library
140 // functions drand48(), lrand48() & company; generates a
141 // uniformly distributed sequence.
142 //--------------------------------------------------------
152 Rand48 (unsigned long int seed = 0);
155 //--------------------------------
156 // Re-initialize with a given seed
157 //--------------------------------
159 void init (unsigned long int seed);
162 //----------------------------------------------------------
163 // Get the next value in the sequence (range: [false, true])
164 //----------------------------------------------------------
169 //---------------------------------------------------------------
170 // Get the next value in the sequence (range: [0 ... 0x7fffffff])
171 //---------------------------------------------------------------
176 //------------------------------------------------------
177 // Get the next value in the sequence (range: [0 ... 1[)
178 //------------------------------------------------------
183 //-------------------------------------------------------------------
184 // Get the next value in the sequence (range [rangeMin ... rangeMax[)
185 //-------------------------------------------------------------------
187 double nextf (double rangeMin, double rangeMax);
192 unsigned short int _state[3];
194 #if defined ( _WIN32 ) || defined ( _WIN64 ) || defined ( __MWERKS__ )
200 //------------------------------------------------------------
201 // Return random points uniformly distributed in a sphere with
202 // radius 1 around the origin (distance from origin <= 1).
203 //------------------------------------------------------------
205 template <class Vec, class Rand>
207 solidSphereRand (Rand &rand);
210 //-------------------------------------------------------------
211 // Return random points uniformly distributed on the surface of
212 // a sphere with radius 1 around the origin.
213 //-------------------------------------------------------------
215 template <class Vec, class Rand>
217 hollowSphereRand (Rand &rand);
220 //-----------------------------------------------
221 // Return random numbers with a normal (Gaussian)
222 // distribution with zero mean and unit variance.
223 //-----------------------------------------------
225 template <class Rand>
227 gaussRand (Rand &rand);
230 //----------------------------------------------------
231 // Return random points whose distance from the origin
232 // has a normal (Gaussian) distribution with zero mean
233 // and unit variance.
234 //----------------------------------------------------
236 template <class Vec, class Rand>
238 gaussSphereRand (Rand &rand);
247 Rand32::init (unsigned long int seed)
249 _state = (seed * 0xa5a573a5L) ^ 0x5a5a5a5aL;
254 Rand32::Rand32 (unsigned long int seed)
263 _state = 1664525L * _state + 1013904223L;
271 // Return the 31st (most significant) bit, by and-ing with 2 ^ 31.
272 return !!(_state & 2147483648UL);
276 inline unsigned long int
280 return _state & 0xffffffff;
288 return ((int) (_state & 0xffffff)) * ((float) (1.0F / 0x1000000));
293 Rand32::nextf (float rangeMin, float rangeMax)
295 return rangeMin + nextf() * (rangeMax - rangeMin);
300 Rand48::init (unsigned long int seed)
302 seed = (seed * 0xa5a573a5L) ^ 0x5a5a5a5aL;
304 _state[0] = (unsigned short int) (seed);
305 _state[1] = (unsigned short int) (seed >> 16);
306 _state[2] = (unsigned short int) (seed);
311 Rand48::Rand48 (unsigned long int seed)
317 #if defined ( _WIN32 ) || defined ( _WIN64 ) || defined ( __MWERKS__ )
323 unsigned short temp[2];
325 accu = 0xe66dUL * ( unsigned long )_state[0] + 0x000bUL;
327 temp[0] = ( unsigned short )accu; /* lower 16 bits */
328 accu >>= sizeof( unsigned short ) * 8;
330 accu += 0xe66dUL * ( unsigned long )_state[1] +
331 0xdeecUL * ( unsigned long )_state[0];
333 temp[1] = ( unsigned short )accu; /* middle 16 bits */
334 accu >>= sizeof( unsigned short ) * 8;
336 accu += 0xe66dUL * _state[2] +
337 0xdeecUL * _state[1] +
338 0x0005UL * _state[0];
342 _state[2] = ( unsigned short )accu;
350 #if defined ( _WIN32 ) || defined ( _WIN64 ) || defined ( __MWERKS__ )
352 return ( ( long( _state[2] ) << 15 ) + ( long( _state[1] ) >> 1 ) ) & 0x1;
354 return nrand48 (_state) & 1;
362 #if defined ( _WIN32 ) || defined ( _WIN64 ) || defined ( __MWERKS__ )
364 return ( long( _state[2] ) << 15 ) + ( long( _state[1] ) >> 1 );
366 return nrand48 (_state);
374 #if defined ( _WIN32 ) || defined ( _WIN64 ) || defined ( __MWERKS__ )
376 return ldexp( double( _state[0] ), -48 ) +
377 ldexp( double( _state[1] ), -32 ) +
378 ldexp( double( _state[2] ), -16 );
380 return erand48 (_state);
386 Rand48::nextf (double rangeMin, double rangeMax)
388 return rangeMin + nextf() * (rangeMax - rangeMin);
392 template <class Vec, class Rand>
394 solidSphereRand (Rand &rand)
400 for (unsigned int i = 0; i < Vec::dimensions(); i++)
401 v[i] = (typename Vec::BaseType) rand.nextf (-1, 1);
403 while (v.length2() > 1);
409 template <class Vec, class Rand>
411 hollowSphereRand (Rand &rand)
414 typename Vec::BaseType length;
418 for (unsigned int i = 0; i < Vec::dimensions(); i++)
419 v[i] = (typename Vec::BaseType) rand.nextf (-1, 1);
423 while (length > 1 || length == 0);
429 template <class Rand>
431 gaussRand (Rand &rand)
433 float x; // Note: to avoid numerical problems with very small
434 float y; // numbers, we make these variables singe-precision
435 float length2; // floats, but later we call the double-precision log()
436 // and sqrt() functions instead of logf() and sqrtf().
439 x = float (rand.nextf (-1, 1));
440 y = float (rand.nextf (-1, 1));
441 length2 = x * x + y * y;
443 while (length2 >= 1 || length2 == 0);
445 return x * sqrt (-2 * log (length2) / length2);
449 template <class Vec, class Rand>
451 gaussSphereRand (Rand &rand)
453 return hollowSphereRand <Vec> (rand) * gaussRand (rand);