quick_rng.h

Go to the documentation of this file.
00001 /* === S Y N F I G ========================================================= */
00022 /* ========================================================================= */
00023 
00024 /* === S T A R T =========================================================== */
00025 
00026 #ifndef __SYNFIG_QUICK_RNG_H
00027 #define __SYNFIG_QUICK_RNG_H
00028 
00029 /* === H E A D E R S ======================================================= */
00030 
00031 #include <stdint.h>
00032 
00033 /* === M A C R O S ========================================================= */
00034 
00035 /* === T Y P E D E F S ===================================================== */
00036 
00037 /* === C L A S S E S & S T R U C T S ======================================= */
00038 
00039 // A fast 32-bit linear congruential random number generator
00040 class quick_rng
00041 {
00042     uint32_t next;
00043 public:
00044     quick_rng(uint32_t seed=0):next(seed) { }
00045 
00046     void set_seed(uint32_t x)
00047     {
00048         next=x;
00049     }
00050 
00051     uint32_t i32()
00052     {
00053         static const uint32_t a(1664525);
00054         static const uint32_t c(1013904223);
00055 
00056         return next=next*a+c;
00057     }
00058 
00059     uint32_t i16()
00060     {
00061         return i32()>>16;
00062     }
00063 
00064     float f()
00065     {
00066         static const float m(int(65535));
00067 
00068         return float(i16())/m;
00069     }
00070 
00071     uint32_t operator()(const uint32_t& m)
00072     {
00073         if(m==65536)
00074             return i16();
00075         else
00076         if(m<=65536)
00077             return i16()%m;
00078         else
00079             return i32()%m;
00080     }
00081 };
00082 
00083 /* === E N D =============================================================== */
00084 
00085 #endif

Generated on Wed Dec 12 03:11:41 2007 for synfig by  doxygen 1.5.4