00001 // Randomizer.h -- Interface for a random number generator. 00002 00003 /* 00004 * Copyright (C) 1997, 1998, 2004 Tudor Hulubei <tudor@hulubei.net>. 00005 * 00006 * This library is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as 00008 * published by the Free Software Foundation; either version 2, or (at 00009 * your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA, 00019 * 02111-1307, USA. 00020 */ 00021 00022 /* 00023 * NOTE: This is an interal header file. 00024 * You should not attempt to use it directly. 00025 */ 00026 00027 // $Id: Randomizer_8h-source.html,v 1.1 2005/05/25 12:37:18 tudor Exp $ 00028 00029 #ifndef _CSP_KERNEL_Randomizer_H 00030 #define _CSP_KERNEL_Randomizer_H 00031 00032 00033 #include <cstdlib> 00034 #include <cmath> 00035 00036 00037 CSP_NAMESPACE_BEGIN(csp); 00038 00039 00040 /* 00041 * In Numerical Recipes in C: The Art of Scientific Computing (William H. 00042 * Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; 00043 * New York: Cambridge University Press, 1990 (1st ed, p. 207)), the 00044 * following comments are made: 00045 * 00046 * "If you want to generate a random integer between 1 and 10, you should 00047 * always do it by: 00048 * 00049 * j = 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0)); 00050 * 00051 * and never by anything resembling 00052 * 00053 * j = 1 + ((int)(1000000.0 * rand()) % 10); 00054 * 00055 * (which uses lower-order bits)." 00056 * 00057 * ... which is what we do when we use the system's pseudo-random 00058 * number generator. However, to make it easier for other researchers 00059 * to reproduce our results (as well as for us to reproduce theirs), 00060 * and to make sure that given a set of parameters the same problem is 00061 * generated on different platforms, the default is to use William 00062 * H. Press, et al.'s random number generator from "Numerical Recipes 00063 * in C", Second Edition with corrections (1994) page 282. 00064 */ 00065 00066 class CSP_API Randomizer 00067 { 00068 public: 00077 Randomizer(bool systemPRNG = false) : 00078 m_systemPRNG(systemPRNG) {} 00079 00085 void setSeed(unsigned int seed) 00086 { 00087 if (m_systemPRNG) 00088 srand(seed); 00089 else 00090 m_seed = -abs(static_cast<int>(seed)); 00091 } 00092 00100 int next(int limit) 00101 { 00102 if (m_systemPRNG) 00103 return int(double(limit) * rand() / (RAND_MAX + 1.0)); 00104 else 00105 return static_cast<int>(ran2(&m_seed) * limit); 00106 } 00107 00108 private: 00115 float ran2(long* idum); 00116 00118 bool m_systemPRNG; 00119 00121 long m_seed; 00122 }; 00123 00124 00125 CSP_NAMESPACE_END(csp); 00126 00127 00128 #endif // _CSP_KERNEL_Randomizer_H 00129 00130 // Local Variables: 00131 // mode: C++ 00132 // End:
1.3.9.1