blob: dc9e681c5a72c0bb2186cb67206e90b5a06c6734 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkScalar.h"
14
reed@google.com57212f92013-12-30 14:40:38 +000015#ifdef SK_SUPPORT_LEGACY_SK64
16 #include "Sk64.h"
17#endif
18
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000019/** \class SkLCGRandom
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
21 Utility class that implements pseudo random 32bit numbers using a fast
22 linear equation. Unlike rand(), this class holds its own seed (initially
23 set to 0), so that multiple instances can be used with no side-effects.
24*/
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000025class SkLCGRandom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026public:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000027 SkLCGRandom() : fSeed(0) {}
28 SkLCGRandom(uint32_t seed) : fSeed(seed) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
30 /** Return the next pseudo random number as an unsigned 32bit value.
31 */
32 uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; }
33
34 /** Return the next pseudo random number as a signed 32bit value.
35 */
36 int32_t nextS() { return (int32_t)this->nextU(); }
37
38 /** Return the next pseudo random number as an unsigned 16bit value.
39 */
40 U16CPU nextU16() { return this->nextU() >> 16; }
41
42 /** Return the next pseudo random number as a signed 16bit value.
43 */
44 S16CPU nextS16() { return this->nextS() >> 16; }
45
tfarina@chromium.org223137f2012-11-21 22:38:36 +000046 /**
47 * Returns value [0...1) as a float
48 */
49 float nextF() {
50 // const is 1 / (2^32 - 1)
51 return (float)(this->nextU() * 2.32830644e-10);
52 }
53
54 /**
55 * Returns value [min...max) as a float
56 */
57 float nextRangeF(float min, float max) {
58 return min + this->nextF() * (max - min);
59 }
60
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 /** Return the next pseudo random number, as an unsigned value of
62 at most bitCount bits.
63 @param bitCount The maximum number of bits to be returned
64 */
65 uint32_t nextBits(unsigned bitCount) {
66 SkASSERT(bitCount > 0 && bitCount <= 32);
67 return this->nextU() >> (32 - bitCount);
68 }
69
70 /** Return the next pseudo random unsigned number, mapped to lie within
71 [min, max] inclusive.
72 */
73 uint32_t nextRangeU(uint32_t min, uint32_t max) {
74 SkASSERT(min <= max);
bsalomon@google.com6d5d08f2013-01-25 19:19:20 +000075 uint32_t range = max - min + 1;
76 if (0 == range) {
77 return this->nextU();
78 } else {
79 return min + this->nextU() % range;
80 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 }
82
bsalomon@google.comd4726202012-08-03 14:34:46 +000083 /** Return the next pseudo random unsigned number, mapped to lie within
84 [0, count).
85 */
86 uint32_t nextULessThan(uint32_t count) {
87 SkASSERT(count > 0);
88 return this->nextRangeU(0, count - 1);
89 }
90
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 /** Return the next pseudo random number expressed as an unsigned SkFixed
92 in the range [0..SK_Fixed1).
93 */
94 SkFixed nextUFixed1() { return this->nextU() >> 16; }
95
96 /** Return the next pseudo random number expressed as a signed SkFixed
97 in the range (-SK_Fixed1..SK_Fixed1).
98 */
99 SkFixed nextSFixed1() { return this->nextS() >> 15; }
100
101 /** Return the next pseudo random number expressed as a SkScalar
102 in the range [0..SK_Scalar1).
103 */
104 SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); }
105
106 /** Return the next pseudo random number expressed as a SkScalar
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000107 in the range [min..max).
108 */
109 SkScalar nextRangeScalar(SkScalar min, SkScalar max) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000110 return SkScalarMul(this->nextUScalar1(), (max - min)) + min;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000111 }
112
113 /** Return the next pseudo random number expressed as a SkScalar
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 in the range (-SK_Scalar1..SK_Scalar1).
115 */
116 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
117
bsalomon@google.comd4726202012-08-03 14:34:46 +0000118 /** Return the next pseudo random number as a bool.
119 */
120 bool nextBool() { return this->nextU() >= 0x80000000; }
121
bsalomon@google.com705e8402012-11-27 15:43:57 +0000122 /** A biased version of nextBool().
123 */
124 bool nextBiasedBool(SkScalar fractionTrue) {
125 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1);
126 return this->nextUScalar1() <= fractionTrue;
127 }
128
reed@google.com57212f92013-12-30 14:40:38 +0000129 /**
130 * Return the next pseudo random number as a signed 64bit value.
131 */
132 int64_t next64() {
133 int64_t hi = this->nextS();
134 return (hi << 32) | this->nextU();
135 }
136
137#ifdef SK_SUPPORT_LEGACY_SK64
138 SK_ATTR_DEPRECATED("use next64()")
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 void next64(Sk64* a) {
140 SkASSERT(a);
141 a->set(this->nextS(), this->nextU());
142 }
reed@google.com57212f92013-12-30 14:40:38 +0000143#endif
reed@google.com425a8c72012-10-02 17:51:29 +0000144 /**
145 * Return the current seed. This allows the caller to later reset to the
146 * same seed (using setSeed) so it can generate the same sequence.
147 */
148 int32_t getSeed() const { return fSeed; }
149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 /** Set the seed of the random object. The seed is initialized to 0 when the
151 object is first created, and is updated each time the next pseudo random
152 number is requested.
153 */
154 void setSeed(int32_t seed) { fSeed = (uint32_t)seed; }
155
156private:
157 // See "Numerical Recipes in C", 1992 page 284 for these constants
158 enum {
159 kMul = 1664525,
160 kAdd = 1013904223
161 };
162 uint32_t fSeed;
163};
164
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000165/** \class SkRandom
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000166
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000167 Utility class that implements pseudo random 32bit numbers using Marsaglia's
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000168 multiply-with-carry "mother of all" algorithm. Unlike rand(), this class holds
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000169 its own state, so that multiple instances can be used with no side-effects.
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000170
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000171 Has a large period and all bits are well-randomized.
172 */
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000173class SkRandom {
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000174public:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000175 SkRandom() { init(0); }
176 SkRandom(uint32_t seed) { init(seed); }
177 SkRandom(const SkRandom& rand) : fK(rand.fK), fJ(rand.fJ) {}
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000178
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000179 SkRandom& operator=(const SkRandom& rand) {
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000180 fK = rand.fK;
181 fJ = rand.fJ;
182
183 return *this;
184 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000185
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000186 /** Return the next pseudo random number as an unsigned 32bit value.
187 */
188 uint32_t nextU() {
189 fK = kKMul*(fK & 0xffff) + (fK >> 16);
190 fJ = kJMul*(fJ & 0xffff) + (fJ >> 16);
191 return (((fK << 16) | (fK >> 16)) + fJ);
192 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000193
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000194 /** Return the next pseudo random number as a signed 32bit value.
195 */
196 int32_t nextS() { return (int32_t)this->nextU(); }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000197
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000198 /** Return the next pseudo random number as an unsigned 16bit value.
199 */
200 U16CPU nextU16() { return this->nextU() >> 16; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000201
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000202 /** Return the next pseudo random number as a signed 16bit value.
203 */
204 S16CPU nextS16() { return this->nextS() >> 16; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000205
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000206 /**
207 * Returns value [0...1) as an IEEE float
208 */
209 float nextF() {
210 unsigned int floatint = 0x3f800000 | (this->nextU() >> 9);
bsalomon@google.com753a3622013-02-07 19:52:30 +0000211 float f = SkBits2Float(floatint) - 1.0f;
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000212 return f;
213 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000214
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000215 /**
216 * Returns value [min...max) as a float
217 */
218 float nextRangeF(float min, float max) {
219 return min + this->nextF() * (max - min);
220 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000221
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000222 /** Return the next pseudo random number, as an unsigned value of
223 at most bitCount bits.
224 @param bitCount The maximum number of bits to be returned
225 */
226 uint32_t nextBits(unsigned bitCount) {
227 SkASSERT(bitCount > 0 && bitCount <= 32);
228 return this->nextU() >> (32 - bitCount);
229 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000230
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000231 /** Return the next pseudo random unsigned number, mapped to lie within
232 [min, max] inclusive.
233 */
234 uint32_t nextRangeU(uint32_t min, uint32_t max) {
235 SkASSERT(min <= max);
236 uint32_t range = max - min + 1;
237 if (0 == range) {
238 return this->nextU();
239 } else {
240 return min + this->nextU() % range;
241 }
242 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000243
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000244 /** Return the next pseudo random unsigned number, mapped to lie within
245 [0, count).
246 */
247 uint32_t nextULessThan(uint32_t count) {
248 SkASSERT(count > 0);
249 return this->nextRangeU(0, count - 1);
250 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000251
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000252 /** Return the next pseudo random number expressed as an unsigned SkFixed
253 in the range [0..SK_Fixed1).
254 */
255 SkFixed nextUFixed1() { return this->nextU() >> 16; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000256
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000257 /** Return the next pseudo random number expressed as a signed SkFixed
258 in the range (-SK_Fixed1..SK_Fixed1).
259 */
260 SkFixed nextSFixed1() { return this->nextS() >> 15; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000261
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000262 /** Return the next pseudo random number expressed as a SkScalar
263 in the range [0..SK_Scalar1).
264 */
265 SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000266
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000267 /** Return the next pseudo random number expressed as a SkScalar
268 in the range [min..max).
269 */
270 SkScalar nextRangeScalar(SkScalar min, SkScalar max) {
271 return SkScalarMul(this->nextUScalar1(), (max - min)) + min;
272 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000273
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000274 /** Return the next pseudo random number expressed as a SkScalar
275 in the range (-SK_Scalar1..SK_Scalar1).
276 */
277 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
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 bool.
280 */
281 bool nextBool() { return this->nextU() >= 0x80000000; }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000282
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000283 /** A biased version of nextBool().
284 */
285 bool nextBiasedBool(SkScalar fractionTrue) {
286 SkASSERT(fractionTrue >= 0 && fractionTrue <= SK_Scalar1);
287 return this->nextUScalar1() <= fractionTrue;
288 }
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000289
reed@google.com57212f92013-12-30 14:40:38 +0000290 /**
291 * Return the next pseudo random number as a signed 64bit value.
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000292 */
reed@google.com57212f92013-12-30 14:40:38 +0000293 int64_t next64() {
294 int64_t hi = this->nextS();
295 return (hi << 32) | this->nextU();
296 }
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000297
reed@google.com57212f92013-12-30 14:40:38 +0000298#ifdef SK_SUPPORT_LEGACY_SK64
299 SK_ATTR_DEPRECATED("use next64()")
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000300 void next64(Sk64* a) {
301 SkASSERT(a);
302 a->set(this->nextS(), this->nextU());
303 }
reed@google.com57212f92013-12-30 14:40:38 +0000304#endif
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000305
306 /** Reset the random object.
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000307 */
308 void setSeed(uint32_t seed) { init(seed); }
309
310private:
311 // Initialize state variables with LCG.
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000312 // We must ensure that both J and K are non-zero, otherwise the
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000313 // multiply-with-carry step will forevermore return zero.
314 void init(uint32_t seed) {
315 fK = NextLCG(seed);
316 if (0 == fK) {
317 fK = NextLCG(fK);
318 }
319 fJ = NextLCG(fK);
320 if (0 == fJ) {
321 fJ = NextLCG(fJ);
322 }
323 SkASSERT(0 != fK && 0 != fJ);
324 }
325 static uint32_t NextLCG(uint32_t seed) { return kMul*seed + kAdd; }
326
327 // See "Numerical Recipes in C", 1992 page 284 for these constants
328 // For the LCG that sets the initial state from a seed
329 enum {
330 kMul = 1664525,
331 kAdd = 1013904223
332 };
333 // Constants for the multiply-with-carry steps
334 enum {
335 kKMul = 30345,
336 kJMul = 18000,
337 };
skia.committer@gmail.com24d5ee42013-01-31 07:06:15 +0000338
jvanverth@google.coma8e66f72013-01-30 15:42:19 +0000339 uint32_t fK;
340 uint32_t fJ;
341};
342
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343#endif