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