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" | |
14 #include "SkScalar.h" | 13 #include "SkScalar.h" |
15 | 14 |
| 15 #ifdef SK_SUPPORT_LEGACY_SK64 |
| 16 #include "Sk64.h" |
| 17 #endif |
| 18 |
16 /** \class SkLCGRandom | 19 /** \class SkLCGRandom |
17 | 20 |
18 Utility class that implements pseudo random 32bit numbers using a fast | 21 Utility class that implements pseudo random 32bit numbers using a fast |
19 linear equation. Unlike rand(), this class holds its own seed (initially | 22 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. | 23 set to 0), so that multiple instances can be used with no side-effects. |
21 */ | 24 */ |
22 class SkLCGRandom { | 25 class SkLCGRandom { |
23 public: | 26 public: |
24 SkLCGRandom() : fSeed(0) {} | 27 SkLCGRandom() : fSeed(0) {} |
25 SkLCGRandom(uint32_t seed) : fSeed(seed) {} | 28 SkLCGRandom(uint32_t seed) : fSeed(seed) {} |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 */ | 119 */ |
117 bool nextBool() { return this->nextU() >= 0x80000000; } | 120 bool nextBool() { return this->nextU() >= 0x80000000; } |
118 | 121 |
119 /** A biased version of nextBool(). | 122 /** A biased version of nextBool(). |
120 */ | 123 */ |
121 bool nextBiasedBool(SkScalar fractionTrue) { | 124 bool nextBiasedBool(SkScalar fractionTrue) { |
122 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); | 125 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); |
123 return this->nextUScalar1() <= fractionTrue; | 126 return this->nextUScalar1() <= fractionTrue; |
124 } | 127 } |
125 | 128 |
126 /** Return the next pseudo random number as a signed 64bit value. | 129 /** |
127 */ | 130 * Return the next pseudo random number as a signed 64bit value. |
| 131 */ |
| 132 int64_t next64() { |
| 133 int64_t hi = this->nextS(); |
| 134 return (hi << 32) | this->nextU(); |
| 135 } |
| 136 |
| 137 #ifdef SK_SUPPORT_LEGACY_SK64 |
| 138 SK_ATTR_DEPRECATED("use next64()") |
128 void next64(Sk64* a) { | 139 void next64(Sk64* a) { |
129 SkASSERT(a); | 140 SkASSERT(a); |
130 a->set(this->nextS(), this->nextU()); | 141 a->set(this->nextS(), this->nextU()); |
131 } | 142 } |
132 | 143 #endif |
133 /** | 144 /** |
134 * Return the current seed. This allows the caller to later reset to the | 145 * Return the current seed. This allows the caller to later reset to the |
135 * same seed (using setSeed) so it can generate the same sequence. | 146 * same seed (using setSeed) so it can generate the same sequence. |
136 */ | 147 */ |
137 int32_t getSeed() const { return fSeed; } | 148 int32_t getSeed() const { return fSeed; } |
138 | 149 |
139 /** Set the seed of the random object. The seed is initialized to 0 when the | 150 /** Set the seed of the random object. The seed is initialized to 0 when the |
140 object is first created, and is updated each time the next pseudo random | 151 object is first created, and is updated each time the next pseudo random |
141 number is requested. | 152 number is requested. |
142 */ | 153 */ |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 */ | 280 */ |
270 bool nextBool() { return this->nextU() >= 0x80000000; } | 281 bool nextBool() { return this->nextU() >= 0x80000000; } |
271 | 282 |
272 /** A biased version of nextBool(). | 283 /** A biased version of nextBool(). |
273 */ | 284 */ |
274 bool nextBiasedBool(SkScalar fractionTrue) { | 285 bool nextBiasedBool(SkScalar fractionTrue) { |
275 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); | 286 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); |
276 return this->nextUScalar1() <= fractionTrue; | 287 return this->nextUScalar1() <= fractionTrue; |
277 } | 288 } |
278 | 289 |
279 /** Return the next pseudo random number as a signed 64bit value. | 290 /** |
| 291 * Return the next pseudo random number as a signed 64bit value. |
280 */ | 292 */ |
| 293 int64_t next64() { |
| 294 int64_t hi = this->nextS(); |
| 295 return (hi << 32) | this->nextU(); |
| 296 } |
| 297 |
| 298 #ifdef SK_SUPPORT_LEGACY_SK64 |
| 299 SK_ATTR_DEPRECATED("use next64()") |
281 void next64(Sk64* a) { | 300 void next64(Sk64* a) { |
282 SkASSERT(a); | 301 SkASSERT(a); |
283 a->set(this->nextS(), this->nextU()); | 302 a->set(this->nextS(), this->nextU()); |
284 } | 303 } |
| 304 #endif |
285 | 305 |
286 /** Reset the random object. | 306 /** Reset the random object. |
287 */ | 307 */ |
288 void setSeed(uint32_t seed) { init(seed); } | 308 void setSeed(uint32_t seed) { init(seed); } |
289 | 309 |
290 private: | 310 private: |
291 // Initialize state variables with LCG. | 311 // Initialize state variables with LCG. |
292 // We must ensure that both J and K are non-zero, otherwise the | 312 // We must ensure that both J and K are non-zero, otherwise the |
293 // multiply-with-carry step will forevermore return zero. | 313 // multiply-with-carry step will forevermore return zero. |
294 void init(uint32_t seed) { | 314 void init(uint32_t seed) { |
(...skipping 19 matching lines...) Expand all Loading... |
314 enum { | 334 enum { |
315 kKMul = 30345, | 335 kKMul = 30345, |
316 kJMul = 18000, | 336 kJMul = 18000, |
317 }; | 337 }; |
318 | 338 |
319 uint32_t fK; | 339 uint32_t fK; |
320 uint32_t fJ; | 340 uint32_t fJ; |
321 }; | 341 }; |
322 | 342 |
323 #endif | 343 #endif |
OLD | NEW |