Created by Scott Robert Ladd at Coyote Gulch Productions.
00001 //--------------------------------------------------------------------- 00002 // Algorithmic Conjurings @ http://www.coyotegulch.com 00003 // 00004 // prng.h (libcoyotl) 00005 // 00006 // A Framework for Pseudorandom Number Generators 00007 //--------------------------------------------------------------------- 00008 // 00009 // Copyright 1990-2005 Scott Robert Ladd 00010 // 00011 // This program is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU General Public License as published by 00013 // the Free Software Foundation; either version 2 of the License, or 00014 // (at your option) any later version. 00015 // 00016 // This program is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU General Public License 00022 // along with this program; if not, write to the 00023 // Free Software Foundation, Inc. 00024 // 59 Temple Place - Suite 330 00025 // Boston, MA 02111-1307, USA. 00026 // 00027 //----------------------------------------------------------------------- 00028 // 00029 // For more information on this software package, please visit 00030 // Scott's web site, Coyote Gulch Productions, at: 00031 // 00032 // http://www.coyotegulch.com 00033 // 00034 //----------------------------------------------------------------------- 00035 00036 #if !defined(LIBCOYOTL_PRNG_H) 00037 #define LIBCOYOTL_PRNG_H 00038 00039 // Standard C Library 00040 #include <cstddef> 00041 #include <cstdio> 00042 #include <ctime> 00043 #include <cmath> 00044 #include <fcntl.h> 00045 #include <stdint.h> 00046 00047 namespace libcoyotl 00048 { 00050 00054 class prng 00055 { 00056 protected: 00058 uint32_t m_seed; 00059 00060 public: 00062 00066 prng(); 00067 00069 00075 prng(uint32_t seed); 00076 00078 00082 virtual void init(uint32_t seed); 00083 00085 00089 uint32_t get_seed(); 00090 00092 00096 virtual uint32_t get_rand() = 0; 00097 00099 00105 uint32_t get_rand_range(uint32_t lo, uint32_t hi); 00106 00108 00113 size_t get_rand_index(size_t length); 00114 00116 00122 double get_rand_real1(); 00123 00125 00131 double get_rand_real2(); 00132 00134 00140 double get_rand_real3(); 00141 00143 00149 double get_rand_real53(); 00150 }; 00151 00152 //--------------------------------------------------------------------------- 00153 // Returns the original seed value 00154 inline uint32_t prng::get_seed() 00155 { 00156 return m_seed; 00157 } 00158 00159 //--------------------------------------------------------------------------- 00160 // Obtain a psuedorandom integer in the range [lo,hi] 00161 inline uint32_t prng::get_rand_range(uint32_t lo, uint32_t hi) 00162 { 00163 // Local working storage 00164 double range = hi - lo + 1.0; 00165 00166 // Use real value to caluclate range 00167 return lo + uint32_t(floor(range * get_rand_real2())); 00168 } 00169 00170 //-------------------------------------------------------------------------- 00171 // Returns the next value as a size_t "index" in the range [0,length). 00172 inline size_t prng::get_rand_index(size_t length) 00173 { 00174 return size_t(double(length) * get_rand_real2()); 00175 } 00176 00177 //-------------------------------------------------------------------------- 00178 // Obtain a psuedorandom real number in the range [0,1], i.e., a number 00179 // greater than or equal to 0 and less than or equal to 1. 00180 inline double prng::get_rand_real1() 00181 { 00182 // privides a granularity of approx. 2.3E-10 00183 return double(get_rand()) * (1.0 / 4294967295.0); 00184 } 00185 00186 //-------------------------------------------------------------------------- 00187 // Obtain a psuedorandom real number in the range [0,1), i.e., a number 00188 // greater than or equal to 0 and less than 1. 00189 inline double prng::get_rand_real2() 00190 { 00191 // privides a granularity of approx. 2.3E-10 00192 return double(get_rand()) * (1.0 / 4294967296.0); 00193 } 00194 00195 //-------------------------------------------------------------------------- 00196 // Obtain a psuedorandom real number in the range (0,1), i.e., a number 00197 // greater than 0 and less than 1. 00198 inline double prng::get_rand_real3() 00199 { 00200 // privides a granularity of approx. 2.3E-10 00201 return double((double(get_rand()) + 0.5) * (1.0 / 4294967296.0)); 00202 } 00203 00204 } // end namespace libcoyotl 00205 00206 #endif
© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.