| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkRandom_DEFINED |
| 11 | #define SkRandom_DEFINED |
| 12 | |
| 13 | #include "Sk64.h" |
| 14 | #include "SkScalar.h" |
| 15 | |
| 16 | /** \class SkRandom |
| 17 | |
| 18 | Utility class that implements pseudo random 32bit numbers using a fast |
| 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. |
| 21 | */ |
| 22 | class SkRandom { |
| 23 | public: |
| 24 | SkRandom() : fSeed(0) {} |
| 25 | SkRandom(uint32_t seed) : fSeed(seed) {} |
| 26 | |
| 27 | /** Return the next pseudo random number as an unsigned 32bit value. |
| 28 | */ |
| 29 | uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; } |
| 30 | |
| 31 | /** Return the next pseudo random number as a signed 32bit value. |
| 32 | */ |
| 33 | int32_t nextS() { return (int32_t)this->nextU(); } |
| 34 | |
| 35 | /** Return the next pseudo random number as an unsigned 16bit value. |
| 36 | */ |
| 37 | U16CPU nextU16() { return this->nextU() >> 16; } |
| 38 | |
| 39 | /** Return the next pseudo random number as a signed 16bit value. |
| 40 | */ |
| 41 | S16CPU nextS16() { return this->nextS() >> 16; } |
| 42 | |
| tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Returns value [0...1) as a float |
| 45 | */ |
| 46 | float nextF() { |
| 47 | // const is 1 / (2^32 - 1) |
| 48 | return (float)(this->nextU() * 2.32830644e-10); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns value [min...max) as a float |
| 53 | */ |
| 54 | float nextRangeF(float min, float max) { |
| 55 | return min + this->nextF() * (max - min); |
| 56 | } |
| 57 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | /** Return the next pseudo random number, as an unsigned value of |
| 59 | at most bitCount bits. |
| 60 | @param bitCount The maximum number of bits to be returned |
| 61 | */ |
| 62 | uint32_t nextBits(unsigned bitCount) { |
| 63 | SkASSERT(bitCount > 0 && bitCount <= 32); |
| 64 | return this->nextU() >> (32 - bitCount); |
| 65 | } |
| 66 | |
| 67 | /** Return the next pseudo random unsigned number, mapped to lie within |
| 68 | [min, max] inclusive. |
| 69 | */ |
| 70 | uint32_t nextRangeU(uint32_t min, uint32_t max) { |
| 71 | SkASSERT(min <= max); |
| 72 | return min + this->nextU() % (max - min + 1); |
| 73 | } |
| 74 | |
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 75 | /** Return the next pseudo random unsigned number, mapped to lie within |
| 76 | [0, count). |
| 77 | */ |
| 78 | uint32_t nextULessThan(uint32_t count) { |
| 79 | SkASSERT(count > 0); |
| 80 | return this->nextRangeU(0, count - 1); |
| 81 | } |
| 82 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | /** Return the next pseudo random number expressed as an unsigned SkFixed |
| 84 | in the range [0..SK_Fixed1). |
| 85 | */ |
| 86 | SkFixed nextUFixed1() { return this->nextU() >> 16; } |
| 87 | |
| 88 | /** Return the next pseudo random number expressed as a signed SkFixed |
| 89 | in the range (-SK_Fixed1..SK_Fixed1). |
| 90 | */ |
| 91 | SkFixed nextSFixed1() { return this->nextS() >> 15; } |
| 92 | |
| 93 | /** Return the next pseudo random number expressed as a SkScalar |
| 94 | in the range [0..SK_Scalar1). |
| 95 | */ |
| 96 | SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); } |
| 97 | |
| 98 | /** Return the next pseudo random number expressed as a SkScalar |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 99 | in the range [min..max). |
| 100 | */ |
| 101 | SkScalar nextRangeScalar(SkScalar min, SkScalar max) { |
| bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 102 | return SkScalarMul(this->nextUScalar1(), (max - min)) + min; |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** Return the next pseudo random number expressed as a SkScalar |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | in the range (-SK_Scalar1..SK_Scalar1). |
| 107 | */ |
| 108 | SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } |
| 109 | |
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 110 | /** Return the next pseudo random number as a bool. |
| 111 | */ |
| 112 | bool nextBool() { return this->nextU() >= 0x80000000; } |
| 113 | |
| bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 114 | /** A biased version of nextBool(). |
| 115 | */ |
| 116 | bool nextBiasedBool(SkScalar fractionTrue) { |
| 117 | SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); |
| 118 | return this->nextUScalar1() <= fractionTrue; |
| 119 | } |
| 120 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | /** Return the next pseudo random number as a signed 64bit value. |
| 122 | */ |
| 123 | void next64(Sk64* a) { |
| 124 | SkASSERT(a); |
| 125 | a->set(this->nextS(), this->nextU()); |
| 126 | } |
| 127 | |
| reed@google.com | 425a8c7 | 2012-10-02 17:51:29 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Return the current seed. This allows the caller to later reset to the |
| 130 | * same seed (using setSeed) so it can generate the same sequence. |
| 131 | */ |
| 132 | int32_t getSeed() const { return fSeed; } |
| 133 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | /** Set the seed of the random object. The seed is initialized to 0 when the |
| 135 | object is first created, and is updated each time the next pseudo random |
| 136 | number is requested. |
| 137 | */ |
| 138 | void setSeed(int32_t seed) { fSeed = (uint32_t)seed; } |
| 139 | |
| 140 | private: |
| 141 | // See "Numerical Recipes in C", 1992 page 284 for these constants |
| 142 | enum { |
| 143 | kMul = 1664525, |
| 144 | kAdd = 1013904223 |
| 145 | }; |
| 146 | uint32_t fSeed; |
| 147 | }; |
| 148 | |
| 149 | #endif |
| 150 | |