HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathRandom.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
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
16 // distribution.
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.
20 //
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.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 #ifndef INCLUDED_IMATHRANDOM_H
37 #define INCLUDED_IMATHRANDOM_H
38 
39 //-----------------------------------------------------------------------------
40 //
41 // Generators for uniformly distributed pseudo-random numbers and
42 // functions that use those generators to generate numbers with
43 // non-uniform distributions:
44 //
45 // class Rand32
46 // class Rand48
47 // solidSphereRand()
48 // hollowSphereRand()
49 // gaussRand()
50 // gaussSphereRand()
51 //
52 // Note: class Rand48() calls erand48() and nrand48(), which are not
53 // available on all operating systems. For compatibility we include
54 // our own versions of erand48() and nrand48(). Our functions have
55 // been reverse-engineered from the corresponding Unix/Linux man page.
56 //
57 //-----------------------------------------------------------------------------
58 
59 #include "ImathNamespace.h"
60 #include "ImathExport.h"
61 
62 #include <stdlib.h>
63 #include <math.h>
64 
66 
67 //-----------------------------------------------
68 // Fast random-number generator that generates
69 // a uniformly distributed sequence with a period
70 // length of 2^32.
71 //-----------------------------------------------
72 
74 {
75  public:
76 
77  //------------
78  // Constructor
79  //------------
80 
81  Rand32 (unsigned long int seed = 0);
82 
83 
84  //--------------------------------
85  // Re-initialize with a given seed
86  //--------------------------------
87 
88  void init (unsigned long int seed);
89 
90 
91  //----------------------------------------------------------
92  // Get the next value in the sequence (range: [false, true])
93  //----------------------------------------------------------
94 
95  bool nextb ();
96 
97 
98  //---------------------------------------------------------------
99  // Get the next value in the sequence (range: [0 ... 0xffffffff])
100  //---------------------------------------------------------------
101 
102  unsigned long int nexti ();
103 
104 
105  //------------------------------------------------------
106  // Get the next value in the sequence (range: [0 ... 1[)
107  //------------------------------------------------------
108 
109  float nextf ();
110 
111 
112  //-------------------------------------------------------------------
113  // Get the next value in the sequence (range [rangeMin ... rangeMax[)
114  //-------------------------------------------------------------------
115 
116  float nextf (float rangeMin, float rangeMax);
117 
118 
119  private:
120 
121  void next ();
122 
123  unsigned long int _state;
124 };
125 
126 
127 //--------------------------------------------------------
128 // Random-number generator based on the C Standard Library
129 // functions erand48(), nrand48() & company; generates a
130 // uniformly distributed sequence.
131 //--------------------------------------------------------
132 
133 class Rand48
134 {
135  public:
136 
137  //------------
138  // Constructor
139  //------------
140 
141  Rand48 (unsigned long int seed = 0);
142 
143 
144  //--------------------------------
145  // Re-initialize with a given seed
146  //--------------------------------
147 
148  void init (unsigned long int seed);
149 
150 
151  //----------------------------------------------------------
152  // Get the next value in the sequence (range: [false, true])
153  //----------------------------------------------------------
154 
155  bool nextb ();
156 
157 
158  //---------------------------------------------------------------
159  // Get the next value in the sequence (range: [0 ... 0x7fffffff])
160  //---------------------------------------------------------------
161 
162  long int nexti ();
163 
164 
165  //------------------------------------------------------
166  // Get the next value in the sequence (range: [0 ... 1[)
167  //------------------------------------------------------
168 
169  double nextf ();
170 
171 
172  //-------------------------------------------------------------------
173  // Get the next value in the sequence (range [rangeMin ... rangeMax[)
174  //-------------------------------------------------------------------
175 
176  double nextf (double rangeMin, double rangeMax);
177 
178 
179  private:
180 
181  unsigned short int _state[3];
182 };
183 
184 
185 //------------------------------------------------------------
186 // Return random points uniformly distributed in a sphere with
187 // radius 1 around the origin (distance from origin <= 1).
188 //------------------------------------------------------------
189 
190 template <class Vec, class Rand>
191 Vec
192 solidSphereRand (Rand &rand);
193 
194 
195 //-------------------------------------------------------------
196 // Return random points uniformly distributed on the surface of
197 // a sphere with radius 1 around the origin.
198 //-------------------------------------------------------------
199 
200 template <class Vec, class Rand>
201 Vec
202 hollowSphereRand (Rand &rand);
203 
204 
205 //-----------------------------------------------
206 // Return random numbers with a normal (Gaussian)
207 // distribution with zero mean and unit variance.
208 //-----------------------------------------------
209 
210 template <class Rand>
211 float
212 gaussRand (Rand &rand);
213 
214 
215 //----------------------------------------------------
216 // Return random points whose distance from the origin
217 // has a normal (Gaussian) distribution with zero mean
218 // and unit variance.
219 //----------------------------------------------------
220 
221 template <class Vec, class Rand>
222 Vec
223 gaussSphereRand (Rand &rand);
224 
225 
226 //---------------------------------
227 // erand48(), nrand48() and friends
228 //---------------------------------
229 
230 IMATH_EXPORT double erand48 (unsigned short state[3]);
231 IMATH_EXPORT double drand48 ();
232 IMATH_EXPORT long int nrand48 (unsigned short state[3]);
233 IMATH_EXPORT long int lrand48 ();
234 IMATH_EXPORT void srand48 (long int seed);
235 
236 
237 //---------------
238 // Implementation
239 //---------------
240 
241 
242 inline void
243 Rand32::init (unsigned long int seed)
244 {
245  _state = (seed * 0xa5a573a5L) ^ 0x5a5a5a5aL;
246 }
247 
248 
249 inline
250 Rand32::Rand32 (unsigned long int seed)
251 {
252  init (seed);
253 }
254 
255 
256 inline void
257 Rand32::next ()
258 {
259  _state = 1664525L * _state + 1013904223L;
260 }
261 
262 
263 inline bool
265 {
266  next ();
267  // Return the 31st (most significant) bit, by and-ing with 2 ^ 31.
268  return !!(_state & 2147483648UL);
269 }
270 
271 
272 inline unsigned long int
274 {
275  next ();
276  return _state & 0xffffffff;
277 }
278 
279 
280 inline float
281 Rand32::nextf (float rangeMin, float rangeMax)
282 {
283  float f = nextf();
284  return rangeMin * (1 - f) + rangeMax * f;
285 }
286 
287 
288 inline void
289 Rand48::init (unsigned long int seed)
290 {
291  seed = (seed * 0xa5a573a5L) ^ 0x5a5a5a5aL;
292 
293  _state[0] = (unsigned short int) (seed & 0xFFFF);
294  _state[1] = (unsigned short int) ((seed >> 16) & 0xFFFF);
295  _state[2] = (unsigned short int) (seed & 0xFFFF);
296 }
297 
298 
299 inline
300 Rand48::Rand48 (unsigned long int seed)
301 {
302  init (seed);
303 }
304 
305 
306 inline bool
308 {
309  return nrand48 (_state) & 1;
310 }
311 
312 
313 inline long int
315 {
316  return nrand48 (_state);
317 }
318 
319 
320 inline double
322 {
323  return erand48 (_state);
324 }
325 
326 
327 inline double
328 Rand48::nextf (double rangeMin, double rangeMax)
329 {
330  double f = nextf();
331  return rangeMin * (1 - f) + rangeMax * f;
332 }
333 
334 
335 template <class Vec, class Rand>
336 Vec
337 solidSphereRand (Rand &rand)
338 {
339  Vec v;
340 
341  do
342  {
343  for (unsigned int i = 0; i < Vec::dimensions(); i++)
344  v[i] = (typename Vec::BaseType) rand.nextf (-1, 1);
345  }
346  while (v.length2() > 1);
347 
348  return v;
349 }
350 
351 
352 template <class Vec, class Rand>
353 Vec
354 hollowSphereRand (Rand &rand)
355 {
356  Vec v;
357  typename Vec::BaseType length;
358 
359  do
360  {
361  for (unsigned int i = 0; i < Vec::dimensions(); i++)
362  v[i] = (typename Vec::BaseType) rand.nextf (-1, 1);
363 
364  length = v.length();
365  }
366  while (length > 1 || length == 0);
367 
368  return v / length;
369 }
370 
371 
372 template <class Rand>
373 float
374 gaussRand (Rand &rand)
375 {
376  float x; // Note: to avoid numerical problems with very small
377  float y; // numbers, we make these variables singe-precision
378  float length2; // floats, but later we call the double-precision log()
379  // and sqrt() functions instead of logf() and sqrtf().
380  do
381  {
382  x = float (rand.nextf (-1, 1));
383  y = float (rand.nextf (-1, 1));
384  length2 = x * x + y * y;
385  }
386  while (length2 >= 1 || length2 == 0);
387 
388  return x * sqrt (-2 * log (double (length2)) / length2);
389 }
390 
391 
392 template <class Vec, class Rand>
393 Vec
394 gaussSphereRand (Rand &rand)
395 {
396  return hollowSphereRand <Vec> (rand) * gaussRand (rand);
397 }
398 
400 
401 #endif // INCLUDED_IMATHRANDOM_H
bool nextb()
Definition: ImathRandom.h:264
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
long int nexti()
Definition: ImathRandom.h:314
void init(unsigned long int seed)
Definition: ImathRandom.h:289
Rand48(unsigned long int seed=0)
Definition: ImathRandom.h:300
Rand32(unsigned long int seed=0)
Definition: ImathRandom.h:250
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
double nextf()
Definition: ImathRandom.h:321
const GLdouble * v
Definition: glcorearb.h:836
Vec solidSphereRand(Rand &rand)
Definition: ImathRandom.h:337
GLint y
Definition: glcorearb.h:102
IMATH_EXPORT double erand48(unsigned short state[3])
png_uint_32 i
Definition: png.h:2877
Vec hollowSphereRand(Rand &rand)
Definition: ImathRandom.h:354
SYS_API double log(double x)
Definition: SYS_FPUMath.h:87
void init(unsigned long int seed)
Definition: ImathRandom.h:243
GLfloat f
Definition: glcorearb.h:1925
float nextf()
Vec gaussSphereRand(Rand &rand)
Definition: ImathRandom.h:394
unsigned long int nexti()
Definition: ImathRandom.h:273
#define IMATH_EXPORT
Definition: ImathExport.h:60
IMATH_EXPORT long int lrand48()
typedef int
Definition: png.h:1175
GLint GLenum GLint x
Definition: glcorearb.h:408
bool nextb()
Definition: ImathRandom.h:307
float gaussRand(Rand &rand)
Definition: ImathRandom.h:374
IMATH_EXPORT long int nrand48(unsigned short state[3])
IMATH_EXPORT double drand48()
IMATH_EXPORT void srand48(long int seed)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794