00001 // Filename: randomizer.I 00002 // Created by: drose (18Jan07) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: Randomizer::Constructor 00018 // Access: Public 00019 // Description: If seed is nonzero, it is used to define the tables; 00020 // if it is zero a random seed is generated. 00021 //////////////////////////////////////////////////////////////////// 00022 Randomizer:: 00023 Randomizer(unsigned long seed) : 00024 _mersenne(seed != 0 ? seed : get_next_seed()) 00025 { 00026 } 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: Randomizer::Copy Constructor 00030 // Access: Public 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 Randomizer:: 00034 Randomizer(const Randomizer ©) : 00035 _mersenne(copy._mersenne) 00036 { 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: Randomizer::Copy Assignment Operator 00041 // Access: Public 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 void Randomizer:: 00045 operator = (const Randomizer ©) { 00046 _mersenne = copy._mersenne; 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: Randomizer::random_int 00051 // Access: Public 00052 // Description: Returns a random integer in the range [0, range). 00053 //////////////////////////////////////////////////////////////////// 00054 INLINE int Randomizer:: 00055 random_int(int range) { 00056 return (int)floor(random_real((double)range)); 00057 } 00058 00059 //////////////////////////////////////////////////////////////////// 00060 // Function: Randomizer::random_real 00061 // Access: Public 00062 // Description: Returns a random double in the range [0, range). 00063 //////////////////////////////////////////////////////////////////// 00064 INLINE double Randomizer:: 00065 random_real(double range) { 00066 return (range * _mersenne.get_uint31()) / ((double)0x80000000); 00067 } 00068 00069 //////////////////////////////////////////////////////////////////// 00070 // Function: Randomizer::random_real_unit 00071 // Access: Public 00072 // Description: Returns a random double in the range [-0.5, 0.5). 00073 //////////////////////////////////////////////////////////////////// 00074 INLINE double Randomizer:: 00075 random_real_unit() { 00076 return random_real(1.0f) - 0.5f; 00077 } 00078 00079 //////////////////////////////////////////////////////////////////// 00080 // Function: Randomizer::get_next_seed 00081 // Access: Public, Static 00082 // Description: Returns a random seed value for the next global 00083 // Randomizer object. 00084 //////////////////////////////////////////////////////////////////// 00085 INLINE unsigned long Randomizer:: 00086 get_next_seed() { 00087 if (!_got_first_seed) { 00088 _next_seed = Mersenne((unsigned long)time(NULL)); 00089 _got_first_seed = true; 00090 } 00091 return _next_seed.get_uint31(); 00092 } 00093 00094 //////////////////////////////////////////////////////////////////// 00095 // Function: Randomizer::get_seed 00096 // Access: Public 00097 // Description: Returns a unique seed value based on the seed value 00098 // passed to this Randomizer object (and on its current 00099 // state). 00100 //////////////////////////////////////////////////////////////////// 00101 INLINE unsigned long Randomizer:: 00102 get_seed() { 00103 return _mersenne.get_uint31(); 00104 }