| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkRandom_DEFINED | 8 #ifndef SkRandom_DEFINED |
| 9 #define SkRandom_DEFINED | 9 #define SkRandom_DEFINED |
| 10 | 10 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** Return the next pseudo random unsigned number, mapped to lie within | 92 /** Return the next pseudo random unsigned number, mapped to lie within |
| 93 [0, count). | 93 [0, count). |
| 94 */ | 94 */ |
| 95 uint32_t nextULessThan(uint32_t count) { | 95 uint32_t nextULessThan(uint32_t count) { |
| 96 SkASSERT(count > 0); | 96 SkASSERT(count > 0); |
| 97 return this->nextRangeU(0, count - 1); | 97 return this->nextRangeU(0, count - 1); |
| 98 } | 98 } |
| 99 | 99 |
| 100 /** Return the next pseudo random number expressed as an unsigned SkFixed | |
| 101 in the range [0..SK_Fixed1). | |
| 102 */ | |
| 103 SkFixed nextUFixed1() { return this->nextU() >> 16; } | |
| 104 | |
| 105 /** Return the next pseudo random number expressed as a signed SkFixed | |
| 106 in the range (-SK_Fixed1..SK_Fixed1). | |
| 107 */ | |
| 108 SkFixed nextSFixed1() { return this->nextS() >> 15; } | |
| 109 | |
| 110 /** Return the next pseudo random number expressed as a SkScalar | 100 /** Return the next pseudo random number expressed as a SkScalar |
| 111 in the range [0..SK_Scalar1). | 101 in the range [0..SK_Scalar1). |
| 112 */ | 102 */ |
| 113 SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); } | 103 SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); } |
| 114 | 104 |
| 115 /** Return the next pseudo random number expressed as a SkScalar | 105 /** Return the next pseudo random number expressed as a SkScalar |
| 116 in the range [min..max). | 106 in the range [min..max). |
| 117 */ | 107 */ |
| 118 SkScalar nextRangeScalar(SkScalar min, SkScalar max) { | 108 SkScalar nextRangeScalar(SkScalar min, SkScalar max) { |
| 119 return this->nextUScalar1() * (max - min) + min; | 109 return this->nextUScalar1() * (max - min) + min; |
| 120 } | 110 } |
| 121 | 111 |
| 122 /** Return the next pseudo random number expressed as a SkScalar | 112 /** Return the next pseudo random number expressed as a SkScalar |
| 123 in the range (-SK_Scalar1..SK_Scalar1). | 113 in the range [-SK_Scalar1..SK_Scalar1). |
| 124 */ | 114 */ |
| 125 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } | 115 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } |
| 126 | 116 |
| 127 /** Return the next pseudo random number as a bool. | 117 /** Return the next pseudo random number as a bool. |
| 128 */ | 118 */ |
| 129 bool nextBool() { return this->nextU() >= 0x80000000; } | 119 bool nextBool() { return this->nextU() >= 0x80000000; } |
| 130 | 120 |
| 131 /** A biased version of nextBool(). | 121 /** A biased version of nextBool(). |
| 132 */ | 122 */ |
| 133 bool nextBiasedBool(SkScalar fractionTrue) { | 123 bool nextBiasedBool(SkScalar fractionTrue) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 157 fK = NextLCG(fK); | 147 fK = NextLCG(fK); |
| 158 } | 148 } |
| 159 fJ = NextLCG(fK); | 149 fJ = NextLCG(fK); |
| 160 if (0 == fJ) { | 150 if (0 == fJ) { |
| 161 fJ = NextLCG(fJ); | 151 fJ = NextLCG(fJ); |
| 162 } | 152 } |
| 163 SkASSERT(0 != fK && 0 != fJ); | 153 SkASSERT(0 != fK && 0 != fJ); |
| 164 } | 154 } |
| 165 static uint32_t NextLCG(uint32_t seed) { return kMul*seed + kAdd; } | 155 static uint32_t NextLCG(uint32_t seed) { return kMul*seed + kAdd; } |
| 166 | 156 |
| 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 |
| 167 // See "Numerical Recipes in C", 1992 page 284 for these constants | 167 // See "Numerical Recipes in C", 1992 page 284 for these constants |
| 168 // For the LCG that sets the initial state from a seed | 168 // For the LCG that sets the initial state from a seed |
| 169 enum { | 169 enum { |
| 170 kMul = 1664525, | 170 kMul = 1664525, |
| 171 kAdd = 1013904223 | 171 kAdd = 1013904223 |
| 172 }; | 172 }; |
| 173 // Constants for the multiply-with-carry steps | 173 // Constants for the multiply-with-carry steps |
| 174 enum { | 174 enum { |
| 175 kKMul = 30345, | 175 kKMul = 30345, |
| 176 kJMul = 18000, | 176 kJMul = 18000, |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 uint32_t fK; | 179 uint32_t fK; |
| 180 uint32_t fJ; | 180 uint32_t fJ; |
| 181 }; | 181 }; |
| 182 | 182 |
| 183 #endif | 183 #endif |
| OLD | NEW |