Panda3D
randomizer.I
1 // Filename: randomizer.I
2 // Created by: drose (18Jan07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: Randomizer::Constructor
18 // Access: Public
19 // Description: If seed is nonzero, it is used to define the tables;
20 // if it is zero a random seed is generated.
21 ////////////////////////////////////////////////////////////////////
23 Randomizer(unsigned long seed) :
24  _mersenne(seed != 0 ? seed : get_next_seed())
25 {
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: Randomizer::Copy Constructor
30 // Access: Public
31 // Description:
32 ////////////////////////////////////////////////////////////////////
34 Randomizer(const Randomizer &copy) :
35  _mersenne(copy._mersenne)
36 {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: Randomizer::Copy Assignment Operator
41 // Access: Public
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 void Randomizer::
45 operator = (const Randomizer &copy) {
46  _mersenne = copy._mersenne;
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: Randomizer::random_int
51 // Access: Public
52 // Description: Returns a random integer in the range [0, range).
53 ////////////////////////////////////////////////////////////////////
54 INLINE int Randomizer::
55 random_int(int range) {
56  return (int)floor(random_real((double)range));
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: Randomizer::random_real
61 // Access: Public
62 // Description: Returns a random double in the range [0, range).
63 ////////////////////////////////////////////////////////////////////
64 INLINE double Randomizer::
65 random_real(double range) {
66  return (range * _mersenne.get_uint31()) / ((double)0x80000000);
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: Randomizer::random_real_unit
71 // Access: Public
72 // Description: Returns a random double in the range [-0.5, 0.5).
73 ////////////////////////////////////////////////////////////////////
74 INLINE double Randomizer::
76  return random_real(1.0f) - 0.5f;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: Randomizer::get_next_seed
81 // Access: Public, Static
82 // Description: Returns a random seed value for the next global
83 // Randomizer object.
84 ////////////////////////////////////////////////////////////////////
85 INLINE unsigned long Randomizer::
87  if (!_got_first_seed) {
88  _next_seed = Mersenne((unsigned long)time(NULL));
89  _got_first_seed = true;
90  }
91  return _next_seed.get_uint31();
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: Randomizer::get_seed
96 // Access: Public
97 // Description: Returns a unique seed value based on the seed value
98 // passed to this Randomizer object (and on its current
99 // state).
100 ////////////////////////////////////////////////////////////////////
101 INLINE unsigned long Randomizer::
103  return _mersenne.get_uint31();
104 }
double random_real_unit()
Returns a random double in the range [-0.5, 0.5).
Definition: randomizer.I:75
static unsigned long get_next_seed()
Returns a random seed value for the next global Randomizer object.
Definition: randomizer.I:86
unsigned long get_seed()
Returns a unique seed value based on the seed value passed to this Randomizer object (and on its curr...
Definition: randomizer.I:102
double random_real(double range)
Returns a random double in the range [0, range).
Definition: randomizer.I:65
int random_int(int range)
Returns a random integer in the range [0, range).
Definition: randomizer.I:55
A handy class to return random numbers.
Definition: randomizer.h:28
Randomizer(unsigned long seed=0)
If seed is nonzero, it is used to define the tables; if it is zero a random seed is generated...
Definition: randomizer.I:23