libcoyotl - A Library of C++ Tools

Created by Scott Robert Ladd at Coyote Gulch Productions.


prng.h
1//---------------------------------------------------------------------
2// Algorithmic Conjurings @ http://www.coyotegulch.com
3//
4// prng.h (libcoyotl)
5//
6// A Framework for Pseudorandom Number Generators
7//---------------------------------------------------------------------
8//
9// Copyright 1990-2005 Scott Robert Ladd
10//
11// This program is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 2 of the License, or
14// (at your option) any later version.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the
23// Free Software Foundation, Inc.
24// 59 Temple Place - Suite 330
25// Boston, MA 02111-1307, USA.
26//
27//-----------------------------------------------------------------------
28//
29// For more information on this software package, please visit
30// Scott's web site, Coyote Gulch Productions, at:
31//
32// http://www.coyotegulch.com
33//
34//-----------------------------------------------------------------------
35
36#if !defined(LIBCOYOTL_PRNG_H)
37#define LIBCOYOTL_PRNG_H
38
39// Standard C Library
40#include <cstddef>
41#include <cstdio>
42#include <ctime>
43#include <cmath>
44#include <fcntl.h>
45#include <stdint.h>
46
47namespace libcoyotl
48{
50
54 class prng
55 {
56 protected:
59
60 public:
62
67
69
76
78
82 virtual void init(uint32_t seed);
83
85
90
92
96 virtual uint32_t get_rand() = 0;
97
99
106
108
113 size_t get_rand_index(size_t length);
114
116
122 double get_rand_real1();
123
125
131 double get_rand_real2();
132
134
140 double get_rand_real3();
141
143
150 };
151
152 //---------------------------------------------------------------------------
153 // Returns the original seed value
155 {
156 return m_seed;
157 }
158
159 //---------------------------------------------------------------------------
160 // Obtain a psuedorandom integer in the range [lo,hi]
162 {
163 // Local working storage
164 double range = hi - lo + 1.0;
165
166 // Use real value to caluclate range
167 return lo + uint32_t(floor(range * get_rand_real2()));
168 }
169
170 //--------------------------------------------------------------------------
171 // Returns the next value as a size_t "index" in the range [0,length).
172 inline size_t prng::get_rand_index(size_t length)
173 {
174 return size_t(double(length) * get_rand_real2());
175 }
176
177 //--------------------------------------------------------------------------
178 // Obtain a psuedorandom real number in the range [0,1], i.e., a number
179 // greater than or equal to 0 and less than or equal to 1.
180 inline double prng::get_rand_real1()
181 {
182 // privides a granularity of approx. 2.3E-10
183 return double(get_rand()) * (1.0 / 4294967295.0);
184 }
185
186 //--------------------------------------------------------------------------
187 // Obtain a psuedorandom real number in the range [0,1), i.e., a number
188 // greater than or equal to 0 and less than 1.
189 inline double prng::get_rand_real2()
190 {
191 // privides a granularity of approx. 2.3E-10
192 return double(get_rand()) * (1.0 / 4294967296.0);
193 }
194
195 //--------------------------------------------------------------------------
196 // Obtain a psuedorandom real number in the range (0,1), i.e., a number
197 // greater than 0 and less than 1.
198 inline double prng::get_rand_real3()
199 {
200 // privides a granularity of approx. 2.3E-10
201 return double((double(get_rand()) + 0.5) * (1.0 / 4294967296.0));
202 }
203
204} // end namespace libcoyotl
205
206#endif
A STL-compatible array class.
Definition array.h:73
An abstract definition of a peudorandom number generator.
Definition prng.h:55
double get_rand_real3()
Get the next number in the range (0,1)
Definition prng.h:198
prng()
Default constructor, reading seed from/dev/urandom or the time.
double get_rand_real2()
Get the next number in the range [0,1)
Definition prng.h:189
uint32_t get_seed()
Returns the original seed value.
Definition prng.h:154
virtual uint32_t get_rand()=0
Get the next integer.
uint32_t m_seed
Initialization seed.
Definition prng.h:58
uint32_t get_rand_range(uint32_t lo, uint32_t hi)
Get the next integer in the range [lo,hi].
Definition prng.h:161
virtual void init(uint32_t seed)
Initializes the generator with "seed".
double get_rand_real1()
Get the next number in the range [0,1].
Definition prng.h:180
size_t get_rand_index(size_t length)
Get the next random value as a size_t index.
Definition prng.h:172
prng(uint32_t seed)
Default constructor, with optional seed.
double get_rand_real53()
Get the next number in the range [0,1)

© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.