doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Random.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __MATH_RANDOM_H__
30 #define __MATH_RANDOM_H__
31 
32 /*
33 ===============================================================================
34 
35  Random number generator
36 
37 ===============================================================================
38 */
39 
40 class idRandom {
41 public:
42  idRandom( int seed = 0 );
43 
44  void SetSeed( int seed );
45  int GetSeed( void ) const;
46 
47  int RandomInt( void ); // random integer in the range [0, MAX_RAND]
48  int RandomInt( int max ); // random integer in the range [0, max[
49  float RandomFloat( void ); // random number in the range [0.0f, 1.0f]
50  float CRandomFloat( void ); // random number in the range [-1.0f, 1.0f]
51 
52  static const int MAX_RAND = 0x7fff;
53 
54 private:
55  int seed;
56 };
57 
58 ID_INLINE idRandom::idRandom( int seed ) {
59  this->seed = seed;
60 }
61 
62 ID_INLINE void idRandom::SetSeed( int seed ) {
63  this->seed = seed;
64 }
65 
66 ID_INLINE int idRandom::GetSeed( void ) const {
67  return seed;
68 }
69 
70 ID_INLINE int idRandom::RandomInt( void ) {
71  seed = 69069 * seed + 1;
72  return ( seed & idRandom::MAX_RAND );
73 }
74 
75 ID_INLINE int idRandom::RandomInt( int max ) {
76  if ( max == 0 ) {
77  return 0; // avoid divide by zero error
78  }
79  return RandomInt() % max;
80 }
81 
82 ID_INLINE float idRandom::RandomFloat( void ) {
83  return ( RandomInt() / ( float )( idRandom::MAX_RAND + 1 ) );
84 }
85 
86 ID_INLINE float idRandom::CRandomFloat( void ) {
87  return ( 2.0f * ( RandomFloat() - 0.5f ) );
88 }
89 
90 
91 /*
92 ===============================================================================
93 
94  Random number generator
95 
96 ===============================================================================
97 */
98 
99 class idRandom2 {
100 public:
101  idRandom2( unsigned long seed = 0 );
102 
103  void SetSeed( unsigned long seed );
104  unsigned long GetSeed( void ) const;
105 
106  int RandomInt( void ); // random integer in the range [0, MAX_RAND]
107  int RandomInt( int max ); // random integer in the range [0, max]
108  float RandomFloat( void ); // random number in the range [0.0f, 1.0f]
109  float CRandomFloat( void ); // random number in the range [-1.0f, 1.0f]
110 
111  static const int MAX_RAND = 0x7fff;
112 
113 private:
114  unsigned long seed;
115 
116  static const unsigned long IEEE_ONE = 0x3f800000;
117  static const unsigned long IEEE_MASK = 0x007fffff;
118 };
119 
120 ID_INLINE idRandom2::idRandom2( unsigned long seed ) {
121  this->seed = seed;
122 }
123 
124 ID_INLINE void idRandom2::SetSeed( unsigned long seed ) {
125  this->seed = seed;
126 }
127 
128 ID_INLINE unsigned long idRandom2::GetSeed( void ) const {
129  return seed;
130 }
131 
132 ID_INLINE int idRandom2::RandomInt( void ) {
133  seed = 1664525L * seed + 1013904223L;
134  return ( (int) seed & idRandom2::MAX_RAND );
135 }
136 
137 ID_INLINE int idRandom2::RandomInt( int max ) {
138  if ( max == 0 ) {
139  return 0; // avoid divide by zero error
140  }
141  return ( RandomInt() >> ( 16 - idMath::BitsForInteger( max ) ) ) % max;
142 }
143 
144 ID_INLINE float idRandom2::RandomFloat( void ) {
145  unsigned long i;
146  seed = 1664525L * seed + 1013904223L;
148  return ( ( *(float *)&i ) - 1.0f );
149 }
150 
151 ID_INLINE float idRandom2::CRandomFloat( void ) {
152  unsigned long i;
153  seed = 1664525L * seed + 1013904223L;
155  return ( 2.0f * ( *(float *)&i ) - 3.0f );
156 }
157 
158 #endif /* !__MATH_RANDOM_H__ */
idRandom(int seed=0)
Definition: Random.h:58
int RandomInt(void)
Definition: Random.h:132
int seed
Definition: Random.h:55
static const int MAX_RAND
Definition: Random.h:52
float RandomFloat(void)
Definition: Random.h:144
int i
Definition: process.py:33
float CRandomFloat(void)
Definition: Random.h:151
idRandom2(unsigned long seed=0)
Definition: Random.h:120
static const unsigned long IEEE_ONE
Definition: Random.h:116
int RandomInt(void)
Definition: Random.h:70
void SetSeed(int seed)
Definition: Random.h:62
float RandomFloat(void)
Definition: Random.h:82
static const int MAX_RAND
Definition: Random.h:111
Definition: eax4.h:1413
static const unsigned long IEEE_MASK
Definition: Random.h:117
static int BitsForInteger(int i)
Definition: Math.h:727
unsigned long seed
Definition: Random.h:114
tuple f
Definition: idal.py:89
int GetSeed(void) const
Definition: Random.h:66
void SetSeed(unsigned long seed)
Definition: Random.h:124
unsigned long GetSeed(void) const
Definition: Random.h:128
#define max(x, y)
Definition: os.h:70
float CRandomFloat(void)
Definition: Random.h:86