00001 /* 00002 * lib/numerics/rng/libc.h 00003 * 00004 * Interface header for LIBC random() PRNG. 00005 * 00006 * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 00007 * 00008 * This file may be distributed and/or modified under the terms of the 00009 * GNU General Public License version 2 as published by the Free Software 00010 * Foundation. (See COPYING.GPL for details.) 00011 * 00012 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00013 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00014 * 00015 */ 00016 00017 #ifndef _LIB_NUMERICS_RNG_LibCRandom_H_ 00018 #define _LIB_NUMERICS_RNG_LibCRandom_H_ 1 00019 00029 #include <lib/numerics/rng/rng.h> 00030 #include <lib/salloc.h> 00031 00032 00033 namespace NUM // numerics 00034 { 00035 00046 class RandomNumberGenerator_LibCRandom : public RandomNumberGenerator_Base 00047 { 00048 private: 00049 random_data rdata; 00050 char *statebuf; 00051 00052 inline int32 _rand31() 00053 { int32 res; random_r(&rdata,&res); return(res); } 00054 00056 RandomNumberGenerator_LibCRandom( 00057 const RandomNumberGenerator_LibCRandom &); 00059 void operator=(const RandomNumberGenerator_LibCRandom &); 00060 public: 00062 RandomNumberGenerator_LibCRandom(uint32 s) : 00063 RandomNumberGenerator_Base() 00064 { statebuf=NULL; seed(s); } 00066 ~RandomNumberGenerator_LibCRandom() 00067 { FREE(statebuf); } 00068 00075 void seed(uint32 seed) 00076 { srandom_r(seed,&rdata); } 00077 00083 void seed(uint seed,int statelen); 00084 00086 uint32 rand_uint32() // not inline (overriding virtual) 00087 { return( uint32(_rand31()<<2) ^ uint32(_rand31()) ); } 00088 00090 int32 rand_int32() // not inline (overriding virtual) 00091 { return(_rand31()); } 00092 00094 dbl rand_dbl32o() // not inline (overriding virtual) 00095 { return(_rand31()*FPConst<dbl>::div_1_pow_2_31); } 00096 00098 dbl rand_dbl32() // not inline (overriding virtual) 00099 { return(_rand31()*FPConst<dbl>::div_1_pow_2_31m1); } 00100 00102 dbl rand_dbl53o() // not inline (overriding virtual) 00103 { 00104 int32 a=_rand31()>>4, b=_rand31()>>5; 00105 return( (a*67108864.0+b)*FPConst<dbl>::div_1_pow_2_53 ); 00106 } 00107 00109 dbl rand_dbl53() // not inline (overriding virtual) 00110 { 00111 int32 a=_rand31()>>4, b=_rand31()>>5; 00112 return( (a*67108864.0+b)*FPConst<dbl>::div_1_pow_2_53m1 ); 00113 } 00114 }; 00115 00116 } // end of namespace NUM 00117 00118 #endif /* _LIB_NUMERICS_RNG_LibCRandom_H_ */