reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkRandom_DEFINED |
| 9 | #define SkRandom_DEFINED |
| 10 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkScalar.h" |
| 12 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 13 | /** \class SkRandom |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 14 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 15 | Utility class that implements pseudo random 32bit numbers using Marsaglia's |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 16 | multiply-with-carry "mother of all" algorithm. Unlike rand(), this class holds |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 17 | its own state, so that multiple instances can be used with no side-effects. |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 18 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 19 | Has a large period and all bits are well-randomized. |
| 20 | */ |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 21 | class SkRandom { |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 22 | public: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 23 | SkRandom() { init(0); } |
| 24 | SkRandom(uint32_t seed) { init(seed); } |
| 25 | SkRandom(const SkRandom& rand) : fK(rand.fK), fJ(rand.fJ) {} |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 27 | SkRandom& operator=(const SkRandom& rand) { |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 28 | fK = rand.fK; |
| 29 | fJ = rand.fJ; |
| 30 | |
| 31 | return *this; |
| 32 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 33 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 34 | /** Return the next pseudo random number as an unsigned 32bit value. |
| 35 | */ |
| 36 | uint32_t nextU() { |
| 37 | fK = kKMul*(fK & 0xffff) + (fK >> 16); |
| 38 | fJ = kJMul*(fJ & 0xffff) + (fJ >> 16); |
| 39 | return (((fK << 16) | (fK >> 16)) + fJ); |
| 40 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 41 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 42 | /** Return the next pseudo random number as a signed 32bit value. |
| 43 | */ |
| 44 | int32_t nextS() { return (int32_t)this->nextU(); } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 45 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 46 | /** Return the next pseudo random number as an unsigned 16bit value. |
| 47 | */ |
| 48 | U16CPU nextU16() { return this->nextU() >> 16; } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 49 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 50 | /** Return the next pseudo random number as a signed 16bit value. |
| 51 | */ |
| 52 | S16CPU nextS16() { return this->nextS() >> 16; } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 53 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Returns value [0...1) as an IEEE float |
| 56 | */ |
| 57 | float nextF() { |
| 58 | unsigned int floatint = 0x3f800000 | (this->nextU() >> 9); |
bsalomon@google.com | 753a362 | 2013-02-07 19:52:30 +0000 | [diff] [blame] | 59 | float f = SkBits2Float(floatint) - 1.0f; |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 60 | return f; |
| 61 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 62 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Returns value [min...max) as a float |
| 65 | */ |
| 66 | float nextRangeF(float min, float max) { |
| 67 | return min + this->nextF() * (max - min); |
| 68 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 69 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 70 | /** Return the next pseudo random number, as an unsigned value of |
| 71 | at most bitCount bits. |
| 72 | @param bitCount The maximum number of bits to be returned |
| 73 | */ |
| 74 | uint32_t nextBits(unsigned bitCount) { |
| 75 | SkASSERT(bitCount > 0 && bitCount <= 32); |
| 76 | return this->nextU() >> (32 - bitCount); |
| 77 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 78 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 79 | /** Return the next pseudo random unsigned number, mapped to lie within |
| 80 | [min, max] inclusive. |
| 81 | */ |
| 82 | uint32_t nextRangeU(uint32_t min, uint32_t max) { |
| 83 | SkASSERT(min <= max); |
| 84 | uint32_t range = max - min + 1; |
| 85 | if (0 == range) { |
| 86 | return this->nextU(); |
| 87 | } else { |
| 88 | return min + this->nextU() % range; |
| 89 | } |
| 90 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 91 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 92 | /** Return the next pseudo random unsigned number, mapped to lie within |
| 93 | [0, count). |
| 94 | */ |
| 95 | uint32_t nextULessThan(uint32_t count) { |
| 96 | SkASSERT(count > 0); |
| 97 | return this->nextRangeU(0, count - 1); |
| 98 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 99 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 100 | /** Return the next pseudo random number expressed as a SkScalar |
| 101 | in the range [0..SK_Scalar1). |
| 102 | */ |
| 103 | SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 104 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 105 | /** Return the next pseudo random number expressed as a SkScalar |
| 106 | in the range [min..max). |
| 107 | */ |
| 108 | SkScalar nextRangeScalar(SkScalar min, SkScalar max) { |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 109 | return this->nextUScalar1() * (max - min) + min; |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 110 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 111 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 112 | /** Return the next pseudo random number expressed as a SkScalar |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 113 | in the range [-SK_Scalar1..SK_Scalar1). |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 114 | */ |
| 115 | SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 116 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 117 | /** Return the next pseudo random number as a bool. |
| 118 | */ |
| 119 | bool nextBool() { return this->nextU() >= 0x80000000; } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 120 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 121 | /** A biased version of nextBool(). |
| 122 | */ |
| 123 | bool nextBiasedBool(SkScalar fractionTrue) { |
| 124 | SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); |
| 125 | return this->nextUScalar1() <= fractionTrue; |
| 126 | } |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 127 | |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Return the next pseudo random number as a signed 64bit value. |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 130 | */ |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 131 | int64_t next64() { |
| 132 | int64_t hi = this->nextS(); |
| 133 | return (hi << 32) | this->nextU(); |
| 134 | } |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 135 | |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 136 | /** Reset the random object. |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 137 | */ |
| 138 | void setSeed(uint32_t seed) { init(seed); } |
| 139 | |
| 140 | private: |
| 141 | // Initialize state variables with LCG. |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 142 | // We must ensure that both J and K are non-zero, otherwise the |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 143 | // multiply-with-carry step will forevermore return zero. |
| 144 | void init(uint32_t seed) { |
| 145 | fK = NextLCG(seed); |
| 146 | if (0 == fK) { |
| 147 | fK = NextLCG(fK); |
| 148 | } |
| 149 | fJ = NextLCG(fK); |
| 150 | if (0 == fJ) { |
| 151 | fJ = NextLCG(fJ); |
| 152 | } |
| 153 | SkASSERT(0 != fK && 0 != fJ); |
| 154 | } |
| 155 | static uint32_t NextLCG(uint32_t seed) { return kMul*seed + kAdd; } |
| 156 | |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 157 | /** Return the next pseudo random number expressed as an unsigned SkFixed |
| 158 | in the range [0..SK_Fixed1). |
| 159 | */ |
| 160 | SkFixed nextUFixed1() { return this->nextU() >> 16; } |
| 161 | |
| 162 | /** Return the next pseudo random number expressed as a signed SkFixed |
| 163 | in the range [-SK_Fixed1..SK_Fixed1). |
| 164 | */ |
| 165 | SkFixed nextSFixed1() { return this->nextS() >> 15; } |
| 166 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 167 | // See "Numerical Recipes in C", 1992 page 284 for these constants |
| 168 | // For the LCG that sets the initial state from a seed |
| 169 | enum { |
| 170 | kMul = 1664525, |
| 171 | kAdd = 1013904223 |
| 172 | }; |
| 173 | // Constants for the multiply-with-carry steps |
| 174 | enum { |
| 175 | kKMul = 30345, |
| 176 | kJMul = 18000, |
| 177 | }; |
skia.committer@gmail.com | 24d5ee4 | 2013-01-31 07:06:15 +0000 | [diff] [blame] | 178 | |
jvanverth@google.com | a8e66f7 | 2013-01-30 15:42:19 +0000 | [diff] [blame] | 179 | uint32_t fK; |
| 180 | uint32_t fJ; |
| 181 | }; |
| 182 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | #endif |