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

Public Types

typedef unsigned result_type

Public Member Functions

 CRNG32 (const unsigned s)
void seed (const unsigned s)
CRNG32index (const unsigned i)
unsigned sample ()
unsigned sample_range (const unsigned range)
unsigned operator() ()
unsigned hash (unsigned x)

Static Public Member Functions

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

Public Attributes

unsigned n
unsigned key

Detailed Description

Definition at line 12 of file crng.h.

Member Typedef Documentation

◆ result_type

typedef unsigned CRNG32::result_type

Definition at line 39 of file crng.h.

Constructor & Destructor Documentation

◆ CRNG32() [1/2]

CRNG32::CRNG32 ( )
inline

Definition at line 14 of file crng.h.

14: n(), key() { seed(123451); }

◆ CRNG32() [2/2]

CRNG32::CRNG32 ( const unsigned s)
inline

Definition at line 15 of file crng.h.

15: n(), key() { seed(s); }

Member Function Documentation

◆ seed()

void CRNG32::seed ( const unsigned s)
inline

Definition at line 16 of file crng.h.

16{ n= 0; key= (s << 1) | 1; }

◆ index()

CRNG32 & CRNG32::index ( const unsigned i)
inline

Definition at line 18 of file crng.h.

18{ n= i; return *this;}

◆ sample()

unsigned CRNG32::sample ( )
inline

Definition at line 19 of file crng.h.

19{ return hash(++n * key); }

◆ sample_range()

unsigned CRNG32::sample_range ( const unsigned range)
inline

Definition at line 21 of file crng.h.

22 {
23 // Efficiently Generating a Number in a Range
24 // cf http://www.pcg-random.org/posts/bounded-rands.html
25 unsigned divisor= ((-range) / range) + 1; // (2^32) / range
26 if(divisor == 0) return 0;
27
28 while(true)
29 {
30 unsigned x= sample() / divisor;
31 if(x < range) return x;
32 }
33 }

◆ operator()()

unsigned CRNG32::operator() ( )
inline

Definition at line 36 of file crng.h.

36{ return sample(); }

◆ min()

constexpr unsigned CRNG32::min ( )
inlinestaticconstexpr

Definition at line 37 of file crng.h.

37{ return 0; }

◆ max()

constexpr unsigned CRNG32::max ( )
inlinestaticconstexpr

Definition at line 38 of file crng.h.

38{ return ~unsigned(0); }

◆ hash()

unsigned CRNG32::hash ( unsigned x)
inline

Definition at line 42 of file crng.h.

43 {
44 x ^= x >> 16;
45 x *= 0x21f0aaad;
46 x ^= x >> 15;
47 x *= 0xd35a2d97;
48 x ^= x >> 15;
49 return x;
50 }

Member Data Documentation

◆ n

unsigned CRNG32::n

Definition at line 53 of file crng.h.

◆ key

unsigned CRNG32::key

Definition at line 54 of file crng.h.


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