| 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); |
| bsalomon@google.com | 6d5d08f | 2013-01-25 19:19:20 +0000 | [diff] [blame^] | 72 | uint32_t range = max - min + 1; |
| 73 | if (0 == range) { |
| 74 | return this->nextU(); |
| 75 | } else { |
| 76 | return min + this->nextU() % range; |
| 77 | } |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 80 | /** Return the next pseudo random unsigned number, mapped to lie within |
| 81 | [0, count). |
| 82 | */ |
| 83 | uint32_t nextULessThan(uint32_t count) { |
| 84 | SkASSERT(count > 0); |
| 85 | return this->nextRangeU(0, count - 1); |
| 86 | } |
| 87 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | /** Return the next pseudo random number expressed as an unsigned SkFixed |
| 89 | in the range [0..SK_Fixed1). |
| 90 | */ |
| 91 | SkFixed nextUFixed1() { return this->nextU() >> 16; } |
| 92 | |
| 93 | /** Return the next pseudo random number expressed as a signed SkFixed |
| 94 | in the range (-SK_Fixed1..SK_Fixed1). |
| 95 | */ |
| 96 | SkFixed nextSFixed1() { return this->nextS() >> 15; } |
| 97 | |
| 98 | /** Return the next pseudo random number expressed as a SkScalar |
| 99 | in the range [0..SK_Scalar1). |
| 100 | */ |
| 101 | SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); } |
| 102 | |
| 103 | /** Return the next pseudo random number expressed as a SkScalar |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 104 | in the range [min..max). |
| 105 | */ |
| 106 | SkScalar nextRangeScalar(SkScalar min, SkScalar max) { |
| bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 107 | return SkScalarMul(this->nextUScalar1(), (max - min)) + min; |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** Return the next pseudo random number expressed as a SkScalar |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | in the range (-SK_Scalar1..SK_Scalar1). |
| 112 | */ |
| 113 | SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } |
| 114 | |
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 115 | /** Return the next pseudo random number as a bool. |
| 116 | */ |
| 117 | bool nextBool() { return this->nextU() >= 0x80000000; } |
| 118 | |
| bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 119 | /** A biased version of nextBool(). |
| 120 | */ |
| 121 | bool nextBiasedBool(SkScalar fractionTrue) { |
| 122 | SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1); |
| 123 | return this->nextUScalar1() <= fractionTrue; |
| 124 | } |
| 125 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | /** Return the next pseudo random number as a signed 64bit value. |
| 127 | */ |
| 128 | void next64(Sk64* a) { |
| 129 | SkASSERT(a); |
| 130 | a->set(this->nextS(), this->nextU()); |
| 131 | } |
| 132 | |
| reed@google.com | 425a8c7 | 2012-10-02 17:51:29 +0000 | [diff] [blame] | 133 | /** |
| 134 | * 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. |
| 136 | */ |
| 137 | int32_t getSeed() const { return fSeed; } |
| 138 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | /** 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 |
| 141 | number is requested. |
| 142 | */ |
| 143 | void setSeed(int32_t seed) { fSeed = (uint32_t)seed; } |
| 144 | |
| 145 | private: |
| 146 | // See "Numerical Recipes in C", 1992 page 284 for these constants |
| 147 | enum { |
| 148 | kMul = 1664525, |
| 149 | kAdd = 1013904223 |
| 150 | }; |
| 151 | uint32_t fSeed; |
| 152 | }; |
| 153 | |
| 154 | #endif |
| 155 | |