gKit2 light
Loading...
Searching...
No Matches
PCG32 Struct Reference

Public Types

typedef unsigned result_type

Public Member Functions

 PCG32 (const uint64_t s, const uint64_t ss=b)
void seed (const uint64_t s, const uint64_t ss=b)
PCG32index (const uint64_t i)
unsigned sample ()
unsigned sample_range (const unsigned range)
unsigned operator() ()

Static Public Member Functions

static constexpr unsigned min ()
static constexpr unsigned max ()

Public Attributes

uint64_t x
uint64_t x0
uint64_t key

Static Public Attributes

static constexpr uint64_t a = 0x5851f42d4c957f2d
static constexpr uint64_t b = 0xda3e39cb94b95bdb

Detailed Description

Definition at line 12 of file pcg.h.

Member Typedef Documentation

◆ result_type

typedef unsigned PCG32::result_type

Definition at line 82 of file pcg.h.

Constructor & Destructor Documentation

◆ PCG32() [1/2]

PCG32::PCG32 ( )
inline

Definition at line 14 of file pcg.h.

14: x(), x0(), key() { seed(0x853c49e6748fea9b, b); }

◆ PCG32() [2/2]

PCG32::PCG32 ( const uint64_t s,
const uint64_t ss = b )
inline

Definition at line 15 of file pcg.h.

15: x(), x0(), key() { seed(s, ss); }

Member Function Documentation

◆ seed()

void PCG32::seed ( const uint64_t s,
const uint64_t ss = b )
inline

Definition at line 17 of file pcg.h.

18 {
19 key= (ss << 1) | 1;
20
21 x= key + s;
22 sample();
23 x0= x;
24 }

◆ index()

PCG32 & PCG32::index ( const uint64_t i)
inline

Definition at line 26 of file pcg.h.

27 {
28 // advance to sample index
29 // http://www.pcg-random.org implementation
30 uint64_t tmul= a;
31 uint64_t tadd= key;
32 uint64_t mul= 1;
33 uint64_t add= 0;
34
35 uint64_t delta= i;
36 while(delta)
37 {
38 if(delta & 1)
39 {
40 mul= mul * tmul;
41 add= add * tmul + tadd;
42 }
43
44 tadd= tmul * tadd + tadd;
45 tmul= tmul * tmul;
46 delta= delta >> 1;
47 }
48
49 x= mul * x0 + add;
50 return *this;
51 }

◆ sample()

unsigned PCG32::sample ( )
inline

Definition at line 53 of file pcg.h.

54 {
55 uint64_t xx= x;
56 x= a*x + key;
57
58 // hash(x)
59 uint32_t tmp= ((xx >> 18u) ^ xx) >> 27u;
60 uint32_t r= xx >> 59u;
61 return (tmp >> r) | (tmp << ((~r + 1u) & 31));
62 }

◆ sample_range()

unsigned PCG32::sample_range ( const unsigned range)
inline

Definition at line 64 of file pcg.h.

65 {
66 // Efficiently Generating a Number in a Range
67 // cf http://www.pcg-random.org/posts/bounded-rands.html
68 unsigned divisor= ((-range) / range) + 1; // (2^32) / range
69 if(divisor == 0) return 0;
70
71 while(true)
72 {
73 unsigned x= sample() / divisor;
74 if(x < range) return x;
75 }
76 }

◆ operator()()

unsigned PCG32::operator() ( )
inline

Definition at line 79 of file pcg.h.

79{ return sample(); }

◆ min()

constexpr unsigned PCG32::min ( )
inlinestaticconstexpr

Definition at line 80 of file pcg.h.

80{ return 0; }

◆ max()

constexpr unsigned PCG32::max ( )
inlinestaticconstexpr

Definition at line 81 of file pcg.h.

81{ return ~unsigned(0); }

Member Data Documentation

◆ a

uint64_t PCG32::a = 0x5851f42d4c957f2d
staticconstexpr

Definition at line 84 of file pcg.h.

◆ b

uint64_t PCG32::b = 0xda3e39cb94b95bdb
staticconstexpr

Definition at line 85 of file pcg.h.

◆ x

uint64_t PCG32::x

Definition at line 87 of file pcg.h.

◆ x0

uint64_t PCG32::x0

Definition at line 88 of file pcg.h.

◆ key

uint64_t PCG32::key

Definition at line 89 of file pcg.h.


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