Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Rand.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include <random>
26 #include "cinder/Vector.h"
27 
28 namespace cinder {
29 
30 class Rand {
31  public:
32  Rand()
33  : mBase( 214u ), mHaveNextNextGaussian( false )
34  {}
35 
36  Rand( unsigned long seed )
37  : mBase( seed ), mHaveNextNextGaussian( false )
38  {}
39 
41  void seed( unsigned long seedValue );
42 
44  bool nextBool()
45  {
46  return mBase() & 1;
47  }
48 
50  int32_t nextInt()
51  {
52  return mBase();
53  }
54 
56  uint32_t nextUint()
57  {
58  return mBase();
59  }
60 
62  int32_t nextInt( int32_t v )
63  {
64  if( v <= 0 ) return 0;
65  return mBase() % v;
66  }
67 
69  uint32_t nextUint( uint32_t v )
70  {
71  if( v == 0 ) return 0;
72  return mBase() % v;
73  }
74 
76  int32_t nextInt( int32_t a, int32_t b )
77  {
78  return nextInt( b - a ) + a;
79  }
80 
82  float nextFloat()
83  {
84  return mFloatGen(mBase);
85  }
86 
88  float nextFloat( float v )
89  {
90  return mFloatGen(mBase) * v;
91  }
92 
94  float nextFloat( float a, float b )
95  {
96  return mFloatGen(mBase) * ( b - a ) + a;
97  }
98 
100  float posNegFloat( float a, float b )
101  {
102  if( nextBool() )
103  return nextFloat( a, b );
104  else
105  return -nextFloat( a, b );
106  }
107 
110  {
111  float phi = nextFloat( (float)M_PI * 2.0f );
112  float costheta = nextFloat( -1.0f, 1.0f );
113 
114  float rho = math<float>::sqrt( 1.0f - costheta * costheta );
115  float x = rho * math<float>::cos( phi );
116  float y = rho * math<float>::sin( phi );
117  float z = costheta;
118 
119  return Vec3f( x, y, z );
120  }
121 
124  {
125  float theta = nextFloat( (float)M_PI * 2.0f );
126  return Vec2f( math<float>::cos( theta ), math<float>::sin( theta ) );
127  }
128 
130  float nextGaussian()
131  {
132  if( mHaveNextNextGaussian ) {
133  mHaveNextNextGaussian = false;
134  return mNextNextGaussian;
135  }
136  else {
137  float v1, v2, s;
138  do {
139  v1 = 2.0f * nextFloat() - 1.0f;
140  v2 = 2.0f * nextFloat() - 1.0f;
141 
142  s = v1 * v1 + v2 * v2;
143  }
144  while( s >= 1.0f || s == 0.0f );
145 
146  float m = math<float>::sqrt(-2.0f * math<float>::log(s)/s);
147 
148  mNextNextGaussian = v2 * m;
149  mHaveNextNextGaussian = true;
150 
151  return v1 * m;
152  }
153  }
154 
155  // STATICS
157  static void randomize();
158 
160  static void randSeed( unsigned long seedValue );
161 
163  static bool randBool()
164  {
165  return sBase() & 1;
166  }
167 
169  static int32_t randInt()
170  {
171  return sBase();
172  }
173 
175  static uint32_t randUint()
176  {
177  return sBase();
178  }
179 
181  static int32_t randInt( int32_t v )
182  {
183  if( v <= 0 ) return 0;
184  else return sBase() % v;
185  }
186 
188  static uint32_t randUint( uint32_t v )
189  {
190  if( v == 0 ) return 0;
191  else return sBase() % v;
192  }
193 
195  static int32_t randInt( int32_t a, int32_t b )
196  {
197  return randInt( b - a ) + a;
198  }
199 
201  static float randFloat()
202  {
203  return sFloatGen(sBase);
204  }
205 
207  static float randFloat( float v )
208  {
209  return sFloatGen(sBase) * v;
210  }
211 
213  static float randFloat( float a, float b )
214  {
215  return sFloatGen(sBase) * ( b - a ) + a;
216  }
217 
219  static float randPosNegFloat( float a, float b )
220  {
221  if( randBool() )
222  return randFloat( a, b );
223  else
224  return -randFloat( a, b );
225  }
226 
228  static Vec3f randVec3f()
229  {
230  float phi = randFloat( (float)M_PI * 2.0f );
231  float costheta = randFloat( -1.0f, 1.0f );
232 
233  float rho = math<float>::sqrt( 1.0f - costheta * costheta );
234  float x = rho * math<float>::cos( phi );
235  float y = rho * math<float>::sin( phi );
236  float z = costheta;
237 
238  return Vec3f( x, y, z );
239  }
240 
242  static Vec2f randVec2f()
243  {
244  float theta = randFloat( (float)M_PI * 2.0f );
245  return Vec2f( math<float>::cos( theta ), math<float>::sin( theta ) );
246  }
247 
249  static float randGaussian()
250  {
251  static bool sHaveNextNextGaussian = false;
252  static float sNextNextGaussian;
253 
254  if( sHaveNextNextGaussian ) {
255  sHaveNextNextGaussian = false;
256  return sNextNextGaussian;
257  }
258  else {
259  float v1, v2, s;
260  do {
261  v1 = 2.0f * sFloatGen(sBase) - 1.0f;
262  v2 = 2.0f * sFloatGen(sBase) - 1.0f;
263 
264  s = v1 * v1 + v2 * v2;
265  }
266  while( s >= 1.0f || s == 0.0f );
267 
268  float m = math<float>::sqrt(-2.0f * math<float>::log(s)/s);
269 
270  sNextNextGaussian = v2 * m;
271  sHaveNextNextGaussian = true;
272 
273  return v1 * m;
274  }
275  }
276 
277  private:
278  std::mt19937 mBase;
279  std::uniform_real_distribution<float> mFloatGen;
280  float mNextNextGaussian;
281  bool mHaveNextNextGaussian;
282 
283  static std::mt19937 sBase;
284  static std::uniform_real_distribution<float> sFloatGen;
285 };
286 
288 inline void randSeed( unsigned long seedValue ) { Rand::randSeed( seedValue ); }
289 
291 inline bool randBool() { return Rand::randBool(); }
292 
294 inline int32_t randInt() { return Rand::randInt(); }
295 
297 inline int32_t randInt( int32_t v ) { return Rand::randInt( v ); }
298 
300 inline int32_t randInt( int32_t a, int32_t b ) { return Rand::randInt( a, b ); }
301 
303 inline float randFloat() { return Rand::randFloat(); }
304 
306 inline float randFloat( float v ) { return Rand::randFloat( v ); }
307 
309 inline float randFloat( float a, float b ) { return Rand::randFloat( a, b ); }
310 
312 inline float randPosNegFloat( float a, float b ) { return Rand::randPosNegFloat( a, b ); }
313 
315 inline Vec3f randVec3f() { return Rand::randVec3f(); }
316 
318 inline Vec2f randVec2f() { return Rand::randVec2f(); }
319 
321 inline float randGaussian() { return Rand::randGaussian(); }
322 
323 } // namespace cinder
static T sqrt(T x)
Definition: CinderMath.h:63
float nextFloat(float a, float b)
returns a random float in the range [a,b)
Definition: Rand.h:94
float randFloat()
returns a random float in the range [0.0f,1.0f]
Definition: Rand.h:303
void randSeed(unsigned long seedValue)
Resets the static random generator to the specific seed seedValue.
Definition: Rand.h:288
GLenum GLint GLint y
Definition: GLee.h:987
Definition: CinderMath.h:40
static T cos(T x)
Definition: CinderMath.h:46
bool nextBool()
returns a random boolean value
Definition: Rand.h:44
uint32_t nextUint()
returns a random integer in the range [0,4294967296)
Definition: Rand.h:56
static uint32_t randUint(uint32_t v)
returns a random integer in the range [0,v)
Definition: Rand.h:188
static int32_t randInt(int32_t v)
returns a random integer in the range [0,v)
Definition: Rand.h:181
Vec2< float > Vec2f
Definition: Vector.h:1314
static T sin(T x)
Definition: CinderMath.h:47
static uint32_t randUint()
returns a random integer in the range [0,4294967296)
Definition: Rand.h:175
static void randomize()
Resets the static random generator to a random seed based on the clock.
Definition: Rand.cpp:38
int32_t nextInt(int32_t v)
returns a random integer in the range [0,v)
Definition: Rand.h:62
Definition: Rand.h:30
static float randFloat(float a, float b)
returns a random float in the range [a,b)
Definition: Rand.h:213
float posNegFloat(float a, float b)
returns a random float in the range [a,b] or the range [-b,-a)
Definition: Rand.h:100
GLfloat GLfloat v1
Definition: GLee.h:2445
uint32_t nextUint(uint32_t v)
returns a random integer in the range [0,v)
Definition: Rand.h:69
static float randFloat(float v)
returns a random float in the range [0.0f,v)
Definition: Rand.h:207
Vec3< float > Vec3f
Definition: Vector.h:1317
Rand(unsigned long seed)
Definition: Rand.h:36
int32_t randInt()
returns a random integer in the range [0,2147483647]
Definition: Rand.h:294
Rand()
Definition: Rand.h:32
static Vec3f randVec3f()
returns a random Vec3f that represents a point on the unit sphere
Definition: Rand.h:228
float randPosNegFloat(float a, float b)
returns a random float in the range [a,b] or the range [-b,-a]
Definition: Rand.h:312
static float randPosNegFloat(float a, float b)
returns a random float in the range [a,b) or the range [-b,-a)
Definition: Rand.h:219
GLfloat GLfloat GLfloat v2
Definition: GLee.h:2451
int32_t nextInt(int32_t a, int32_t b)
returns a random integer in the range [a,b)
Definition: Rand.h:76
GLenum GLint x
Definition: GLee.h:987
static float randFloat()
returns a random float in the range [0.0f,1.0f)
Definition: Rand.h:201
static float randGaussian()
returns a random float via Gaussian distribution; refactor later
Definition: Rand.h:249
float nextFloat(float v)
returns a random float in the range [0.0f,v)
Definition: Rand.h:88
const GLdouble * v
Definition: GLee.h:1384
Vec3f randVec3f()
returns a random Vec3f that represents a point on the unit sphere
Definition: Rand.h:315
static void randSeed(unsigned long seedValue)
Resets the static random generator to the specific seed seedValue.
Definition: Rand.cpp:49
static bool randBool()
returns a random boolean value
Definition: Rand.h:163
GLdouble GLdouble z
Definition: GLee.h:1911
GLboolean GLboolean GLboolean b
Definition: GLee.h:2964
float nextFloat()
returns a random float in the range [0.0f,1.0f)
Definition: Rand.h:82
Vec3f nextVec3f()
returns a random Vec3f that represents a point on the unit sphere
Definition: Rand.h:109
Vec2f nextVec2f()
returns a random Vec2f that represents a point on the unit circle
Definition: Rand.h:123
#define M_PI
Definition: CinderMath.h:121
GLboolean GLboolean GLboolean GLboolean a
Definition: GLee.h:2964
float randGaussian()
returns a random float via Gaussian distribution
Definition: Rand.h:321
static int32_t randInt(int32_t a, int32_t b)
returns a random integer in the range [a,b)
Definition: Rand.h:195
Vec2f randVec2f()
returns a random Vec2f that represents a point on the unit circle
Definition: Rand.h:318
static int32_t randInt()
returns a random integer in the range [-2147483648,2147483647]
Definition: Rand.h:169
const GLfloat * m
Definition: GLee.h:13493
static Vec2f randVec2f()
returns a random Vec2f that represents a point on the unit circle
Definition: Rand.h:242
GLdouble s
Definition: GLee.h:1378
GLclampf f
Definition: GLee.h:15307
void seed(unsigned long seedValue)
Re-seeds the random generator.
Definition: Rand.cpp:54
int32_t nextInt()
returns a random integer in the range [-2147483648,2147483647]
Definition: Rand.h:50
bool randBool()
returns a random boolean value
Definition: Rand.h:291
float nextGaussian()
returns a random float via Gaussian distribution
Definition: Rand.h:130