blob: 3e2ef201ad42ddbde6daf5b3846afd8149180c3b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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*/
22class SkRandom {
23public:
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.org223137f2012-11-21 22:38:36 +000043 /**
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.com8a1c16f2008-12-17 15:59:43 +000058 /** 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.com6d5d08f2013-01-25 19:19:20 +000072 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.com8a1c16f2008-12-17 15:59:43 +000078 }
79
bsalomon@google.comd4726202012-08-03 14:34:46 +000080 /** 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.com8a1c16f2008-12-17 15:59:43 +000088 /** 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.com0a7672f2012-08-03 18:12:20 +0000104 in the range [min..max).
105 */
106 SkScalar nextRangeScalar(SkScalar min, SkScalar max) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000107 return SkScalarMul(this->nextUScalar1(), (max - min)) + min;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000108 }
109
110 /** Return the next pseudo random number expressed as a SkScalar
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 in the range (-SK_Scalar1..SK_Scalar1).
112 */
113 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
114
bsalomon@google.comd4726202012-08-03 14:34:46 +0000115 /** Return the next pseudo random number as a bool.
116 */
117 bool nextBool() { return this->nextU() >= 0x80000000; }
118
bsalomon@google.com705e8402012-11-27 15:43:57 +0000119 /** 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.com8a1c16f2008-12-17 15:59:43 +0000126 /** 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.com425a8c72012-10-02 17:51:29 +0000133 /**
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.com8a1c16f2008-12-17 15:59:43 +0000139 /** 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
145private:
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
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000154/** \class SkMWCRandom
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000155
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000156 Utility class that implements pseudo random 32bit numbers using Marsaglia's
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000157 multiply-with-carry "mother of all" algorithm. Unlike rand(), this class holds
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000158 its own state, so that multiple instances can be used with no side-effects.
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000159
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000160 Has a large period and all bits are well-randomized.
161 */
162class SkMWCRandom {
163public:
164 SkMWCRandom() { init(0); }
165 SkMWCRandom(uint32_t seed) { init(seed); }
166 SkMWCRandom(const SkMWCRandom& rand) : fK(rand.fK), fJ(rand.fJ) {}
167
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000168 SkMWCRandom& operator=(const SkMWCRandom& rand) {
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000169 fK = rand.fK;
170 fJ = rand.fJ;
171
172 return *this;
173 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000174
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000175 /** Return the next pseudo random number as an unsigned 32bit value.
176 */
177 uint32_t nextU() {
178 fK = kKMul*(fK & 0xffff) + (fK >> 16);
179 fJ = kJMul*(fJ & 0xffff) + (fJ >> 16);
180 return (((fK << 16) | (fK >> 16)) + fJ);
181 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000182
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000183 /** Return the next pseudo random number as a signed 32bit value.
184 */
185 int32_t nextS() { return (int32_t)this->nextU(); }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000186
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000187 /** Return the next pseudo random number as an unsigned 16bit value.
188 */
189 U16CPU nextU16() { return this->nextU() >> 16; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000190
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000191 /** Return the next pseudo random number as a signed 16bit value.
192 */
193 S16CPU nextS16() { return this->nextS() >> 16; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000194
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000195 /**
196 * Returns value [0...1) as an IEEE float
197 */
198 float nextF() {
199 unsigned int floatint = 0x3f800000 | (this->nextU() >> 9);
bsalomon@google.com753a3622013-02-07 19:52:30 +0000200 float f = SkBits2Float(floatint) - 1.0f;
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000201 return f;
202 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000203
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000204 /**
205 * Returns value [min...max) as a float
206 */
207 float nextRangeF(float min, float max) {
208 return min + this->nextF() * (max - min);
209 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000210
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000211 /** Return the next pseudo random number, as an unsigned value of
212 at most bitCount bits.
213 @param bitCount The maximum number of bits to be returned
214 */
215 uint32_t nextBits(unsigned bitCount) {
216 SkASSERT(bitCount > 0 && bitCount <= 32);
217 return this->nextU() >> (32 - bitCount);
218 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000219
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000220 /** Return the next pseudo random unsigned number, mapped to lie within
221 [min, max] inclusive.
222 */
223 uint32_t nextRangeU(uint32_t min, uint32_t max) {
224 SkASSERT(min <= max);
225 uint32_t range = max - min + 1;
226 if (0 == range) {
227 return this->nextU();
228 } else {
229 return min + this->nextU() % range;
230 }
231 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000232
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000233 /** Return the next pseudo random unsigned number, mapped to lie within
234 [0, count).
235 */
236 uint32_t nextULessThan(uint32_t count) {
237 SkASSERT(count > 0);
238 return this->nextRangeU(0, count - 1);
239 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000240
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000241 /** Return the next pseudo random number expressed as an unsigned SkFixed
242 in the range [0..SK_Fixed1).
243 */
244 SkFixed nextUFixed1() { return this->nextU() >> 16; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000245
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000246 /** Return the next pseudo random number expressed as a signed SkFixed
247 in the range (-SK_Fixed1..SK_Fixed1).
248 */
249 SkFixed nextSFixed1() { return this->nextS() >> 15; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000250
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000251 /** Return the next pseudo random number expressed as a SkScalar
252 in the range [0..SK_Scalar1).
253 */
254 SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000255
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000256 /** Return the next pseudo random number expressed as a SkScalar
257 in the range [min..max).
258 */
259 SkScalar nextRangeScalar(SkScalar min, SkScalar max) {
260 return SkScalarMul(this->nextUScalar1(), (max - min)) + min;
261 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000262
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000263 /** Return the next pseudo random number expressed as a SkScalar
264 in the range (-SK_Scalar1..SK_Scalar1).
265 */
266 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000267
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000268 /** Return the next pseudo random number as a bool.
269 */
270 bool nextBool() { return this->nextU() >= 0x80000000; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000271
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000272 /** A biased version of nextBool().
273 */
274 bool nextBiasedBool(SkScalar fractionTrue) {
275 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1);
276 return this->nextUScalar1() <= fractionTrue;
277 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000278
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000279 /** Return the next pseudo random number as a signed 64bit value.
280 */
281 void next64(Sk64* a) {
282 SkASSERT(a);
283 a->set(this->nextS(), this->nextU());
284 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000285
286 /** Reset the random object.
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000287 */
288 void setSeed(uint32_t seed) { init(seed); }
289
290private:
291 // Initialize state variables with LCG.
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000292 // We must ensure that both J and K are non-zero, otherwise the
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000293 // multiply-with-carry step will forevermore return zero.
294 void init(uint32_t seed) {
295 fK = NextLCG(seed);
296 if (0 == fK) {
297 fK = NextLCG(fK);
298 }
299 fJ = NextLCG(fK);
300 if (0 == fJ) {
301 fJ = NextLCG(fJ);
302 }
303 SkASSERT(0 != fK && 0 != fJ);
304 }
305 static uint32_t NextLCG(uint32_t seed) { return kMul*seed + kAdd; }
306
307 // See "Numerical Recipes in C", 1992 page 284 for these constants
308 // For the LCG that sets the initial state from a seed
309 enum {
310 kMul = 1664525,
311 kAdd = 1013904223
312 };
313 // Constants for the multiply-with-carry steps
314 enum {
315 kKMul = 30345,
316 kJMul = 18000,
317 };
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000318
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000319 uint32_t fK;
320 uint32_t fJ;
321};
322
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323#endif