Nsound  0.9.4
Public Member Functions | Private Attributes | List of all members
Nsound::RngTausworthe Class Reference

#include <Nsound/RngTausworthe.h>

Inheritance diagram for Nsound::RngTausworthe:
Inheritance graph
[legend]

Public Member Functions

 RngTausworthe ()
 Default seed used is the number of seconds from unix epoch. More...
 
uint32 get ()
 Get a random number. More...
 
int32 get (const Nsound::int32 min, const Nsound::int32 max)
 Get a random float64 between min & max. More...
 
float64 get (const Nsound::float64 &min, const Nsound::float64 &max)
 Get a random float64 between min & max. More...
 
RngTauswortheoperator= (const RngTausworthe &rhs)
 assignment operator More...
 
void setSeed (Nsound::uint32 seed)
 Set the seed to use. More...
 

Private Attributes

Nsound::uint32 s1_
 
Nsound::uint32 s2_
 
Nsound::uint32 s3_
 

Detailed Description

An implementation of the Tausworthe random number algorithm found in the GNU Scientific Library.

Definition at line 45 of file RngTausworthe.h.

Constructor & Destructor Documentation

RngTausworthe::RngTausworthe ( )

Default seed used is the number of seconds from unix epoch.

Definition at line 40 of file RngTausworthe.cc.

References setSeed().

41  :
42  s1_(0),
43  s2_(0),
44  s3_(0)
45 {
46  setSeed(static_cast<uint32>(time(NULL)));
47 }
Nsound::uint32 s3_
Definition: RngTausworthe.h:76
Nsound::uint32 s1_
Definition: RngTausworthe.h:74
Nsound::uint32 s2_
Definition: RngTausworthe.h:75
void setSeed(Nsound::uint32 seed)
Set the seed to use.

Member Function Documentation

uint32 RngTausworthe::get ( )
virtual

Get a random number.

Implements Nsound::RandomNumberGenerator.

Definition at line 51 of file RngTausworthe.cc.

References s1_, s2_, s3_, and TAUSWORTHE.

Referenced by Nsound::FilterIIR::designKernel(), main(), and Nsound::Kernel::randomize().

52 {
53  #define MASK 0xffffffffUL
54  #define TAUSWORTHE(s,a,b,c,d) (((s & c) << d) & MASK) ^ ((((s << a) & MASK) ^ s) >> b)
55 
56  s1_ = TAUSWORTHE(s1_, 13, 19, 4294967294UL, 12);
57  s2_ = TAUSWORTHE(s2_, 2, 25, 4294967288UL, 4);
58  s3_ = TAUSWORTHE(s3_, 3, 11, 4294967280UL, 17);
59 
60  uint32 r = s1_ ^ s2_ ^ s3_;
61 
62  return r;
63 }
Nsound::uint32 s3_
Definition: RngTausworthe.h:76
unsigned int uint32
Definition: Nsound.h:153
Nsound::uint32 s1_
Definition: RngTausworthe.h:74
Nsound::uint32 s2_
Definition: RngTausworthe.h:75
#define TAUSWORTHE(s, a, b, c, d)
int32 RngTausworthe::get ( const Nsound::int32  min,
const Nsound::int32  max 
)

Get a random float64 between min & max.

Definition at line 67 of file RngTausworthe.cc.

70 {
71  return static_cast<int32>(
72  get(
73  static_cast<float64>(min),
74  static_cast<float64>(max)));
75 }
double float64
Definition: Nsound.h:146
signed int int32
Definition: Nsound.h:142
float64 RngTausworthe::get ( const Nsound::float64 min,
const Nsound::float64 max 
)
virtual

Get a random float64 between min & max.

Implements Nsound::RandomNumberGenerator.

Definition at line 79 of file RngTausworthe.cc.

82 {
83  float64 mag = abs(max - min);
84  float64 d = mag * (static_cast<float64>(get()) / 4294967296.0) + min;
85 
86  return d;
87 }
double float64
Definition: Nsound.h:146
RngTausworthe & RngTausworthe::operator= ( const RngTausworthe rhs)

assignment operator

Definition at line 92 of file RngTausworthe.cc.

References s1_, s2_, and s3_.

93 {
94  if(this == &rhs)
95  {
96  return *this;
97  }
98 
99  s1_ = rhs.s1_;
100  s2_ = rhs.s2_;
101  s3_ = rhs.s3_;
102 
103  return * this;
104 }
Nsound::uint32 s3_
Definition: RngTausworthe.h:76
Nsound::uint32 s1_
Definition: RngTausworthe.h:74
Nsound::uint32 s2_
Definition: RngTausworthe.h:75
void RngTausworthe::setSeed ( Nsound::uint32  seed)
virtual

Set the seed to use.

Implements Nsound::RandomNumberGenerator.

Definition at line 108 of file RngTausworthe.cc.

References LCG, s1_, s2_, and s3_.

Referenced by RngTausworthe().

109 {
110  uint32 s = seed;
111 
112  // Set default seed.
113  if(s == 0)
114  {
115  s = 1;
116  }
117 
118  #define LCG(n) ((69069 * n) & 0xffffffffUL)
119  s1_ = LCG(s);
120  s2_ = LCG(s1_);
121  s3_ = LCG(s2_);
122 
123  // "warm it up"
124  get();
125  get();
126  get();
127  get();
128  get();
129  get();
130 }
Nsound::uint32 s3_
Definition: RngTausworthe.h:76
unsigned int uint32
Definition: Nsound.h:153
#define LCG(n)
Nsound::uint32 s1_
Definition: RngTausworthe.h:74
Nsound::uint32 s2_
Definition: RngTausworthe.h:75

Member Data Documentation

Nsound::uint32 Nsound::RngTausworthe::s1_
private

Definition at line 74 of file RngTausworthe.h.

Referenced by get(), operator=(), and setSeed().

Nsound::uint32 Nsound::RngTausworthe::s2_
private

Definition at line 75 of file RngTausworthe.h.

Referenced by get(), operator=(), and setSeed().

Nsound::uint32 Nsound::RngTausworthe::s3_
private

Definition at line 76 of file RngTausworthe.h.

Referenced by get(), operator=(), and setSeed().


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