00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __UT_Random__
00027 #define __UT_Random__
00028
00029 #include "UT_API.h"
00030 #include <SYS/SYS_Types.h>
00031 #include <SYS/SYS_Math.h>
00032 #include <iostream.h>
00033
00034 class UT_IStream;
00035
00036 class UT_API UT_MersenneTwister {
00037 public:
00038 UT_MersenneTwister(uint seed=5489U)
00039 {
00040 setSeed(seed);
00041 }
00042 ~UT_MersenneTwister() {}
00043
00044 void setSeed(uint seed);
00045 void setSeed(const uint key[], uint klength);
00046
00047 void save(ostream &os, bool binary) const;
00048
00049 bool load(UT_IStream &is);
00050
00051
00052 uint urandom()
00053 {
00054 uint y;
00055 if (myIndex == 624)
00056 {
00057 y = doReload();
00058 myIndex = 1;
00059 }
00060 else
00061 {
00062 y = myState[myIndex++];
00063 }
00064
00065 y ^= (y >> 11);
00066 y ^= (y << 7) & 0x9d2c5680U;
00067 y ^= (y << 15) & 0xefc60000U;
00068 return (y ^ (y >> 18));
00069 }
00070
00071
00072 fpreal32 frandom()
00073 {
00074 SYS_FPReal32Union tmp;
00075 tmp.uval = 0x3f800000 | (0x007fffff & urandom());
00076 return tmp.fval - CONST_FPREAL32(1.0);
00077 }
00078
00079 fpreal32 frandom0()
00080 {
00081 SYS_FPReal32Union tmp;
00082 tmp.uval = 0x3f800000 | (0x007fffff & urandom());
00083 return tmp.fval - CONST_FPREAL32(1.5);
00084 }
00085 private:
00086 uint doReload();
00087
00088 uint myIndex;
00089 uint myState[625];
00090 };
00091
00092 #endif
00093