Home | History | Annotate | Download | only in gpu
      1 
      2 /*
      3  * 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.
      7  */
      8 
      9 
     10 
     11 #ifndef GrRandom_DEFINED
     12 #define GrRandom_DEFINED
     13 
     14 class GrRandom {
     15 public:
     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 
     43 private:
     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 
     56