blob: c98a8fbd94f0475099ae5532d8fc6c739313e856 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrRandom_DEFINED
12#define GrRandom_DEFINED
13
14class GrRandom {
15public:
16 GrRandom() : fSeed(0) {}
17 GrRandom(uint32_t seed) : fSeed(seed) {}
18
19 uint32_t seed() const { return fSeed; }
20
21 uint32_t nextU() {
22 fSeed = fSeed * kMUL + kADD;
23 return fSeed;
24 }
25
26 int32_t nextS() { return (int32_t)this->nextU(); }
27
28 /**
29 * Returns value [0...1) as a float
30 */
31 float nextF() {
32 // const is 1 / (2^32 - 1)
33 return (float)(this->nextU() * 2.32830644e-10);
34 }
35
36 /**
37 * Returns value [min...max) as a float
38 */
39 float nextF(float min, float max) {
40 return min + this->nextF() * (max - min);
41 }
42
43private:
44 /*
45 * These constants taken from "Numerical Recipes in C", reprinted 1999
46 */
47 enum {
48 kMUL = 1664525,
49 kADD = 1013904223
50 };
51 uint32_t fSeed;
52};
53
54#endif
55