Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
SkRandom Class Reference

#include <SkRandom.h>

Public Member Functions

 SkRandom ()
 
 SkRandom (uint32_t seed)
 
 SkRandom (const SkRandom &rand)
 
SkRandomoperator= (const SkRandom &rand)
 
uint32_t nextU ()
 
int32_t nextS ()
 
float nextF ()
 
float nextRangeF (float min, float max)
 
uint32_t nextBits (unsigned bitCount)
 
uint32_t nextRangeU (uint32_t min, uint32_t max)
 
uint32_t nextULessThan (uint32_t count)
 
SkScalar nextUScalar1 ()
 
SkScalar nextRangeScalar (SkScalar min, SkScalar max)
 
SkScalar nextSScalar1 ()
 
bool nextBool ()
 
bool nextBiasedBool (SkScalar fractionTrue)
 
void setSeed (uint32_t seed)
 

Detailed Description

Utility class that implements pseudo random 32bit numbers using Marsaglia's multiply-with-carry "mother of all" algorithm. Unlike rand(), this class holds its own state, so that multiple instances can be used with no side-effects.

Has a large period and all bits are well-randomized.

Definition at line 27 of file SkRandom.h.

Constructor & Destructor Documentation

◆ SkRandom() [1/3]

SkRandom::SkRandom ( )
inline

Definition at line 29 of file SkRandom.h.

29{ init(0); }

◆ SkRandom() [2/3]

SkRandom::SkRandom ( uint32_t  seed)
inline

Definition at line 30 of file SkRandom.h.

30{ init(seed); }

◆ SkRandom() [3/3]

SkRandom::SkRandom ( const SkRandom rand)
inline

Definition at line 31 of file SkRandom.h.

31: fK(rand.fK), fJ(rand.fJ) {}

Member Function Documentation

◆ nextBiasedBool()

bool SkRandom::nextBiasedBool ( SkScalar  fractionTrue)
inline

A biased version of nextBool().

Definition at line 121 of file SkRandom.h.

121 {
122 SkASSERT(fractionTrue >= 0 && fractionTrue <= 1);
123 return this->nextUScalar1() <= fractionTrue;
124 }
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkScalar nextUScalar1()
Definition: SkRandom.h:101

◆ nextBits()

uint32_t SkRandom::nextBits ( unsigned  bitCount)
inline

Return the next pseudo random number, as an unsigned value of at most bitCount bits.

Parameters
bitCountThe maximum number of bits to be returned

Definition at line 72 of file SkRandom.h.

72 {
73 SkASSERT(bitCount > 0 && bitCount <= 32);
74 return this->nextU() >> (32 - bitCount);
75 }
uint32_t nextU()
Definition: SkRandom.h:42

◆ nextBool()

bool SkRandom::nextBool ( )
inline

Return the next pseudo random number as a bool.

Definition at line 117 of file SkRandom.h.

117{ return this->nextU() >= 0x80000000; }

◆ nextF()

float SkRandom::nextF ( )
inline

Returns value [0...1) as an IEEE float

Definition at line 55 of file SkRandom.h.

55 {
56 uint32_t floatint = 0x3f800000 | (this->nextU() >> 9);
57 float f = SkBits2Float(floatint) - 1.0f;
58 return f;
59 }
static float SkBits2Float(uint32_t bits)
Definition: SkFloatBits.h:48

◆ nextRangeF()

float SkRandom::nextRangeF ( float  min,
float  max 
)
inline

Returns value [min...max) as a float

Definition at line 64 of file SkRandom.h.

64 {
65 return min + this->nextF() * (max - min);
66 }
float nextF()
Definition: SkRandom.h:55
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48

◆ nextRangeScalar()

SkScalar SkRandom::nextRangeScalar ( SkScalar  min,
SkScalar  max 
)
inline

Return the next pseudo random number expressed as a SkScalar in the range [min..max).

Definition at line 106 of file SkRandom.h.

106 {
107 return this->nextUScalar1() * (max - min) + min;
108 }

◆ nextRangeU()

uint32_t SkRandom::nextRangeU ( uint32_t  min,
uint32_t  max 
)
inline

Return the next pseudo random unsigned number, mapped to lie within [min, max] inclusive.

Definition at line 80 of file SkRandom.h.

80 {
81 SkASSERT(min <= max);
82 uint32_t range = max - min + 1;
83 if (0 == range) {
84 return this->nextU();
85 } else {
86 return min + this->nextU() % range;
87 }
88 }

◆ nextS()

int32_t SkRandom::nextS ( )
inline

Return the next pseudo random number as a signed 32bit value.

Definition at line 50 of file SkRandom.h.

50{ return (int32_t)this->nextU(); }

◆ nextSScalar1()

SkScalar SkRandom::nextSScalar1 ( )
inline

Return the next pseudo random number expressed as a SkScalar in the range [-SK_Scalar1..SK_Scalar1).

Definition at line 113 of file SkRandom.h.

113{ return SkFixedToScalar(this->nextSFixed1()); }
#define SkFixedToScalar(x)
Definition: SkFixed.h:124

◆ nextU()

uint32_t SkRandom::nextU ( )
inline

Return the next pseudo random number as an unsigned 32bit value.

Definition at line 42 of file SkRandom.h.

42 {
43 fK = kKMul*(fK & 0xffff) + (fK >> 16);
44 fJ = kJMul*(fJ & 0xffff) + (fJ >> 16);
45 return (((fK << 16) | (fK >> 16)) + fJ);
46 }

◆ nextULessThan()

uint32_t SkRandom::nextULessThan ( uint32_t  count)
inline

Return the next pseudo random unsigned number, mapped to lie within [0, count).

Definition at line 93 of file SkRandom.h.

93 {
94 SkASSERT(count > 0);
95 return this->nextRangeU(0, count - 1);
96 }
int count
Definition: FontMgrTest.cpp:50
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition: SkRandom.h:80

◆ nextUScalar1()

SkScalar SkRandom::nextUScalar1 ( )
inline

Return the next pseudo random number expressed as a SkScalar in the range [0..SK_Scalar1).

Definition at line 101 of file SkRandom.h.

101{ return SkFixedToScalar(this->nextUFixed1()); }

◆ operator=()

SkRandom & SkRandom::operator= ( const SkRandom rand)
inline

Definition at line 33 of file SkRandom.h.

33 {
34 fK = rand.fK;
35 fJ = rand.fJ;
36
37 return *this;
38 }

◆ setSeed()

void SkRandom::setSeed ( uint32_t  seed)
inline

Reset the random object.

Definition at line 128 of file SkRandom.h.

128{ init(seed); }

The documentation for this class was generated from the following file: