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