Panda3D
randomizer.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file randomizer.I
10  * @author drose
11  * @date 2007-01-18
12  */
13 
14 /**
15  * If seed is nonzero, it is used to define the tables; if it is zero a random
16  * seed is generated.
17  */
19 Randomizer(unsigned long seed) :
20  _mersenne(seed != 0 ? seed : get_next_seed())
21 {
22 }
23 
24 /**
25  *
26  */
28 Randomizer(const Randomizer &copy) :
29  _mersenne(copy._mersenne)
30 {
31 }
32 
33 /**
34  *
35  */
36 void Randomizer::
37 operator = (const Randomizer &copy) {
38  _mersenne = copy._mersenne;
39 }
40 
41 /**
42  * Returns a random integer in the range [0, range).
43  */
44 INLINE int Randomizer::
45 random_int(int range) {
46  return (int)floor(random_real((double)range));
47 }
48 
49 /**
50  * Returns a random double in the range [0, range).
51  */
52 INLINE double Randomizer::
53 random_real(double range) {
54  return (range * _mersenne.get_uint31()) / ((double)0x80000000);
55 }
56 
57 /**
58  * Returns a random double in the range [-0.5, 0.5).
59  */
60 INLINE double Randomizer::
62  return random_real(1.0f) - 0.5f;
63 }
64 
65 /**
66  * Returns a random seed value for the next global Randomizer object.
67  */
68 INLINE unsigned long Randomizer::
70  if (!_got_first_seed) {
71  _next_seed = Mersenne((unsigned long)time(nullptr));
72  _got_first_seed = true;
73  }
74  return _next_seed.get_uint31();
75 }
76 
77 /**
78  * Returns a unique seed value based on the seed value passed to this
79  * Randomizer object (and on its current state).
80  */
81 INLINE unsigned long Randomizer::
83  return _mersenne.get_uint31();
84 }
double random_real_unit()
Returns a random double in the range [-0.5, 0.5).
Definition: randomizer.I:61
static unsigned long get_next_seed()
Returns a random seed value for the next global Randomizer object.
Definition: randomizer.I:69
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:82
double random_real(double range)
Returns a random double in the range [0, range).
Definition: randomizer.I:53
int random_int(int range)
Returns a random integer in the range [0, range).
Definition: randomizer.I:45
A handy class to return random numbers.
Definition: randomizer.h:26
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:19