gKit2 light
Loading...
Searching...
No Matches
RNGT< UInt, a, c, m > Struct Template Reference

Public Types

typedef unsigned result_type

Public Member Functions

 RNGT (const UInt s)
void seed (const UInt s)
RNGTindex (const UInt 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

UInt x
UInt key

Detailed Description

template<typename UInt, UInt a, UInt c, UInt m>
struct RNGT< UInt, a, c, m >

Definition at line 11 of file rng.h.

Member Typedef Documentation

◆ result_type

template<typename UInt, UInt a, UInt c, UInt m>
typedef unsigned RNGT< UInt, a, c, m >::result_type

Definition at line 62 of file rng.h.

Constructor & Destructor Documentation

◆ RNGT() [1/2]

template<typename UInt, UInt a, UInt c, UInt m>
RNGT< UInt, a, c, m >::RNGT ( )
inline

Definition at line 13 of file rng.h.

13: x(), key() { seed(123451); }

◆ RNGT() [2/2]

template<typename UInt, UInt a, UInt c, UInt m>
RNGT< UInt, a, c, m >::RNGT ( const UInt s)
inline

Definition at line 14 of file rng.h.

14: x(), key() { seed(s); }
Definition rng.h:12

Member Function Documentation

◆ seed()

template<typename UInt, UInt a, UInt c, UInt m>
void RNGT< UInt, a, c, m >::seed ( const UInt s)
inline

Definition at line 15 of file rng.h.

15{ key= (s << 1) | 1; x= key; }

◆ index()

template<typename UInt, UInt a, UInt c, UInt m>
RNGT & RNGT< UInt, a, c, m >::index ( const UInt i)
inline

Definition at line 17 of file rng.h.

18 {
19 UInt tmul= a;
20 UInt tadd= c;
21 UInt mul= 1;
22 UInt add= 0;
23
24 UInt delta= i;
25 while(delta)
26 {
27 if(delta & 1)
28 {
29 mul= mul * tmul;
30 add= add * tmul + tadd;
31 }
32
33 tadd= tmul * tadd + tadd;
34 tmul= tmul * tmul;
35 delta= delta >> 1;
36 }
37
38 x= mul * key + add;
39 return *this;
40 }

◆ sample()

template<typename UInt, UInt a, UInt c, UInt m>
unsigned RNGT< UInt, a, c, m >::sample ( )
inline

Definition at line 42 of file rng.h.

42{ x= (a*x+c) % m; return unsigned(x); }

◆ sample_range()

template<typename UInt, UInt a, UInt c, UInt m>
unsigned RNGT< UInt, a, c, m >::sample_range ( const unsigned range)
inline

Definition at line 44 of file rng.h.

45 {
46 // Efficiently Generating a Number in a Range
47 // cf http://www.pcg-random.org/posts/bounded-rands.html
48 unsigned divisor= ((-range) / range) + 1; // (2^32) / range
49 if(divisor == 0) return 0;
50
51 while(true)
52 {
53 unsigned x= sample() / divisor;
54 if(x < range) return x;
55 }
56 }

◆ operator()()

template<typename UInt, UInt a, UInt c, UInt m>
unsigned RNGT< UInt, a, c, m >::operator() ( )
inline

Definition at line 59 of file rng.h.

59{ return sample(); }

◆ min()

template<typename UInt, UInt a, UInt c, UInt m>
constexpr unsigned RNGT< UInt, a, c, m >::min ( )
inlinestaticconstexpr

Definition at line 60 of file rng.h.

60{ return 0; }

◆ max()

template<typename UInt, UInt a, UInt c, UInt m>
constexpr unsigned RNGT< UInt, a, c, m >::max ( )
inlinestaticconstexpr

Definition at line 61 of file rng.h.

61{ return unsigned(m-1); }

Member Data Documentation

◆ x

template<typename UInt, UInt a, UInt c, UInt m>
UInt RNGT< UInt, a, c, m >::x

Definition at line 65 of file rng.h.

◆ key

template<typename UInt, UInt a, UInt c, UInt m>
UInt RNGT< UInt, a, c, m >::key

Definition at line 66 of file rng.h.


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