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

Public Types

typedef unsigned result_type

Public Member Functions

 CRNG64 (const uint64_t s)
 CRNG64 (const unsigned hi, const unsigned lo=0x40ba6a95)
void seed (const unsigned hi, const unsigned lo=0x40ba6a95)
void seed64 (const uint64_t s)
CRNG64index (const uint64_t i)
unsigned sample ()
unsigned sample_range (const unsigned range)
unsigned operator() ()
unsigned hash (uint64_t v)

Static Public Member Functions

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

Public Attributes

uint64_t n
uint64_t key

Detailed Description

Definition at line 60 of file crng.h.

Member Typedef Documentation

◆ result_type

typedef unsigned CRNG64::result_type

Definition at line 90 of file crng.h.

Constructor & Destructor Documentation

◆ CRNG64() [1/3]

CRNG64::CRNG64 ( )
inline

Definition at line 62 of file crng.h.

62: n(), key() { seed64(0xc58efd154ce32f6d); }

◆ CRNG64() [2/3]

CRNG64::CRNG64 ( const uint64_t s)
inline

Definition at line 63 of file crng.h.

63: n(), key() { seed64(s); }

◆ CRNG64() [3/3]

CRNG64::CRNG64 ( const unsigned hi,
const unsigned lo = 0x40ba6a95 )
inline

Definition at line 64 of file crng.h.

64: n(), key() { seed(hi, lo); }

Member Function Documentation

◆ seed()

void CRNG64::seed ( const unsigned hi,
const unsigned lo = 0x40ba6a95 )
inline

Definition at line 66 of file crng.h.

66{ seed64( uint64_t(hi) << 32 | uint64_t(lo) ); }

◆ seed64()

void CRNG64::seed64 ( const uint64_t s)
inline

Definition at line 67 of file crng.h.

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

◆ index()

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

Definition at line 69 of file crng.h.

69{ n= i; return *this;}

◆ sample()

unsigned CRNG64::sample ( )
inline

Definition at line 70 of file crng.h.

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

◆ sample_range()

unsigned CRNG64::sample_range ( const unsigned range)
inline

Definition at line 72 of file crng.h.

73 {
74 // Efficiently Generating a Number in a Range
75 // cf http://www.pcg-random.org/posts/bounded-rands.html
76 unsigned divisor= ((-range) / range) + 1; // (2^32) / range
77 if(divisor == 0) return 0;
78
79 while(true)
80 {
81 unsigned x= sample() / divisor;
82 if(x < range) return x;
83 }
84 }

◆ operator()()

unsigned CRNG64::operator() ( )
inline

Definition at line 87 of file crng.h.

87{ return sample(); }

◆ min()

constexpr unsigned CRNG64::min ( )
inlinestaticconstexpr

Definition at line 88 of file crng.h.

88{ return 0; }

◆ max()

constexpr unsigned CRNG64::max ( )
inlinestaticconstexpr

Definition at line 89 of file crng.h.

89{ return ~unsigned(0); }

◆ hash()

unsigned CRNG64::hash ( uint64_t v)
inline

Definition at line 92 of file crng.h.

93 {
94 v ^= (v >> 31);
95 v *= 0x7fb5d329728ea185;
96 v ^= (v >> 27);
97 v *= 0x81dadef4bc2dd44d;
98 v ^= (v >> 33);
99 return v;
100 }

Member Data Documentation

◆ n

uint64_t CRNG64::n

Definition at line 103 of file crng.h.

◆ key

uint64_t CRNG64::key

Definition at line 104 of file crng.h.


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