OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkRandom_DEFINED | 10 #ifndef SkRandom_DEFINED |
11 #define SkRandom_DEFINED | 11 #define SkRandom_DEFINED |
12 | 12 |
13 #include "Sk64.h" | 13 #include "Sk64.h" |
14 #include "SkScalar.h" | 14 #include "SkScalar.h" |
15 | 15 |
16 /** \class SkRandom | 16 /** \class SkLCGRandom |
17 | 17 |
18 Utility class that implements pseudo random 32bit numbers using a fast | 18 Utility class that implements pseudo random 32bit numbers using a fast |
19 linear equation. Unlike rand(), this class holds its own seed (initially | 19 linear equation. Unlike rand(), this class holds its own seed (initially |
20 set to 0), so that multiple instances can be used with no side-effects. | 20 set to 0), so that multiple instances can be used with no side-effects. |
21 */ | 21 */ |
22 class SkRandom { | 22 class SkLCGRandom { |
23 public: | 23 public: |
24 SkRandom() : fSeed(0) {} | 24 SkLCGRandom() : fSeed(0) {} |
25 SkRandom(uint32_t seed) : fSeed(seed) {} | 25 SkLCGRandom(uint32_t seed) : fSeed(seed) {} |
26 | 26 |
27 /** Return the next pseudo random number as an unsigned 32bit value. | 27 /** Return the next pseudo random number as an unsigned 32bit value. |
28 */ | 28 */ |
29 uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; } | 29 uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; } |
30 | 30 |
31 /** Return the next pseudo random number as a signed 32bit value. | 31 /** Return the next pseudo random number as a signed 32bit value. |
32 */ | 32 */ |
33 int32_t nextS() { return (int32_t)this->nextU(); } | 33 int32_t nextS() { return (int32_t)this->nextU(); } |
34 | 34 |
35 /** Return the next pseudo random number as an unsigned 16bit value. | 35 /** Return the next pseudo random number as an unsigned 16bit value. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 144 |
145 private: | 145 private: |
146 // See "Numerical Recipes in C", 1992 page 284 for these constants | 146 // See "Numerical Recipes in C", 1992 page 284 for these constants |
147 enum { | 147 enum { |
148 kMul = 1664525, | 148 kMul = 1664525, |
149 kAdd = 1013904223 | 149 kAdd = 1013904223 |
150 }; | 150 }; |
151 uint32_t fSeed; | 151 uint32_t fSeed; |
152 }; | 152 }; |
153 | 153 |
154 /** \class SkMWCRandom | 154 /** \class SkRandom |
155 | 155 |
156 Utility class that implements pseudo random 32bit numbers using Marsaglia's | 156 Utility class that implements pseudo random 32bit numbers using Marsaglia's |
157 multiply-with-carry "mother of all" algorithm. Unlike rand(), this class holds | 157 multiply-with-carry "mother of all" algorithm. Unlike rand(), this class holds |
158 its own state, so that multiple instances can be used with no side-effects. | 158 its own state, so that multiple instances can be used with no side-effects. |
159 | 159 |
160 Has a large period and all bits are well-randomized. | 160 Has a large period and all bits are well-randomized. |
161 */ | 161 */ |
162 class SkMWCRandom { | 162 class SkRandom { |
163 public: | 163 public: |
164 SkMWCRandom() { init(0); } | 164 SkRandom() { init(0); } |
165 SkMWCRandom(uint32_t seed) { init(seed); } | 165 SkRandom(uint32_t seed) { init(seed); } |
166 SkMWCRandom(const SkMWCRandom& rand) : fK(rand.fK), fJ(rand.fJ) {} | 166 SkRandom(const SkRandom& rand) : fK(rand.fK), fJ(rand.fJ) {} |
167 | 167 |
168 SkMWCRandom& operator=(const SkMWCRandom& rand) { | 168 SkRandom& operator=(const SkRandom& rand) { |
169 fK = rand.fK; | 169 fK = rand.fK; |
170 fJ = rand.fJ; | 170 fJ = rand.fJ; |
171 | 171 |
172 return *this; | 172 return *this; |
173 } | 173 } |
174 | 174 |
175 /** Return the next pseudo random number as an unsigned 32bit value. | 175 /** Return the next pseudo random number as an unsigned 32bit value. |
176 */ | 176 */ |
177 uint32_t nextU() { | 177 uint32_t nextU() { |
178 fK = kKMul*(fK & 0xffff) + (fK >> 16); | 178 fK = kKMul*(fK & 0xffff) + (fK >> 16); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 enum { | 314 enum { |
315 kKMul = 30345, | 315 kKMul = 30345, |
316 kJMul = 18000, | 316 kJMul = 18000, |
317 }; | 317 }; |
318 | 318 |
319 uint32_t fK; | 319 uint32_t fK; |
320 uint32_t fJ; | 320 uint32_t fJ; |
321 }; | 321 }; |
322 | 322 |
323 #endif | 323 #endif |
OLD | NEW |