00001 /* 00002 Copyright (c) 2010, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 #pragma once 00024 00025 #include <boost/random.hpp> 00026 #include <boost/random/uniform_int.hpp> 00027 #include <boost/random/uniform_real.hpp> 00028 #include <boost/random/mersenne_twister.hpp> 00029 00030 #include "cinder/Vector.h" 00031 00032 namespace cinder { 00033 00034 class Rand { 00035 public: 00036 Rand() 00037 : mBase( 214u ), mFloatGen( mBase, boost::uniform_real<float>( 0.0f, 1.0f ) ), mIntGen( mBase, boost::uniform_int<>( 0, 2147483647 ) ) 00038 {} 00039 00040 Rand( uint32_t seed ) 00041 : mBase( seed ), mFloatGen( mBase, boost::uniform_real<float>( 0.0f, 1.0f ) ), mIntGen( mBase, boost::uniform_int<>( 0, 2147483647 ) ) 00042 {} 00043 00045 void seed( uint32_t seedValue ); 00046 00048 bool nextBool() 00049 { 00050 return mIntGen() & 1; 00051 } 00052 00054 int32_t nextInt() 00055 { 00056 return mIntGen(); 00057 } 00058 00060 int32_t nextInt( int32_t v ) 00061 { 00062 return mIntGen() % v; 00063 } 00064 00066 int32_t nextInt( int32_t a, int32_t b ) 00067 { 00068 return nextInt( b - a ) + a; 00069 } 00070 00072 float nextFloat() 00073 { 00074 return mFloatGen(); 00075 } 00076 00078 float nextFloat( float v ) 00079 { 00080 return mFloatGen() * v; 00081 } 00082 00084 float nextFloat( float a, float b ) 00085 { 00086 return mFloatGen() * ( b - a ) + a; 00087 } 00088 00090 float posNegFloat( float a, float b ) 00091 { 00092 if( nextBool() ) 00093 return nextFloat( a, b ); 00094 else 00095 return -nextFloat( a, b ); 00096 } 00097 00099 Vec3f nextVec3f() 00100 { 00101 float phi = nextFloat( (float)M_PI * 2.0f ); 00102 float costheta = nextFloat( -1.0f, 1.0f ); 00103 00104 float rho = math<float>::sqrt( 1.0f - costheta * costheta ); 00105 float x = rho * math<float>::cos( phi ); 00106 float y = rho * math<float>::sin( phi ); 00107 float z = costheta; 00108 00109 return Vec3f( x, y, z ); 00110 } 00111 00113 Vec2f nextVec2f( ) 00114 { 00115 float theta = nextFloat( (float)M_PI * 2.0f ); 00116 return Vec2f( math<float>::cos( theta ), math<float>::sin( theta ) ); 00117 } 00118 00119 // STATICS 00121 static void randomize(); 00122 00124 static void randSeed( uint32_t seedValue ); 00125 00127 static bool randBool() 00128 { 00129 return sIntGen() & 1; 00130 } 00131 00133 static int32_t randInt() 00134 { 00135 return sIntGen(); 00136 } 00137 00139 static int32_t randInt( int32_t v ) 00140 { 00141 if( v == 0 ) return 0; 00142 else return sIntGen() % v; 00143 } 00144 00146 static int32_t randInt( int32_t a, int32_t b ) 00147 { 00148 return randInt( b - a ) + a; 00149 } 00150 00152 static float randFloat() 00153 { 00154 return sFloatGen(); 00155 } 00156 00158 static float randFloat( float v ) 00159 { 00160 return sFloatGen() * v; 00161 } 00162 00164 static float randFloat( float a, float b ) 00165 { 00166 return sFloatGen() * ( b - a ) + a; 00167 } 00168 00170 static float randPosNegFloat( float a, float b ) 00171 { 00172 if( randBool() ) 00173 return randFloat( a, b ); 00174 else 00175 return -randFloat( a, b ); 00176 } 00177 00179 static Vec3f randVec3f() 00180 { 00181 float phi = randFloat( (float)M_PI * 2.0f ); 00182 float costheta = randFloat( -1.0f, 1.0f ); 00183 00184 float rho = math<float>::sqrt( 1.0f - costheta * costheta ); 00185 float x = rho * math<float>::cos( phi ); 00186 float y = rho * math<float>::sin( phi ); 00187 float z = costheta; 00188 00189 return Vec3f( x, y, z ); 00190 } 00191 00193 static Vec2f randVec2f() 00194 { 00195 float theta = randFloat( (float)M_PI * 2.0f ); 00196 return Vec2f( math<float>::cos( theta ), math<float>::sin( theta ) ); 00197 } 00198 00199 private: 00200 boost::mt19937 mBase; 00201 boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > mFloatGen; 00202 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > mIntGen; 00203 00204 static boost::mt19937 sBase; 00205 static boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > sFloatGen; 00206 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > sIntGen; 00207 }; 00208 00209 } // namespace cinder