Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

ixlib_random.hh

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //  Description      : Random numbers
00003 // ----------------------------------------------------------------------------
00004 //  (c) Copyright 1996 by iXiONmedia, all rights reserved.
00005 // ----------------------------------------------------------------------------
00006 
00007 
00008 
00009 
00010 #ifndef IXLIB_RANDOM
00011 #define IXLIB_RANDOM
00012 
00013 
00014 
00015 
00016 #include <cstdlib>
00017 #include <cmath>
00018 #include <ctime>
00019 #include <ixlib_base.hh>
00020 #include <ixlib_numeric.hh>
00021 
00022 
00023 
00024 
00025 namespace ixion {
00026   class float_random {
00027     double Seed;
00028   
00029     public:
00030     float_random()
00031       : Seed(1)
00032       { }
00033   
00034     void init() { 
00035       double current_time = time(NULL);
00036       Seed = current_time*sin(current_time); 
00037       }
00038     void init(double seed)
00039       { Seed = NUM_ABS(seed); }
00040 
00042     double operator()(double max = 1) {
00043       // normalize
00044       while (Seed > 3) Seed = log(Seed);
00045       Seed -= floor(Seed);
00046       Seed = pow(Seed+Pi,8);
00047       Seed -= floor(Seed);
00048       return Seed*max;
00049       }
00050     };
00051   
00052   
00053   
00054   
00055   class int_random {
00056       float_random      Generator;
00057   
00058     public:
00059       int_random()
00060         { }
00061     
00062       void init()
00063         { Generator.init(); }
00064       void init(unsigned seed)
00065         { Generator.init(seed); }
00066       
00068       unsigned operator()(unsigned max = 32768) {
00069         unsigned num = rng8() + (rng8() << 7) + (rng8() << 14) + (rng8() << 21) + (rng8() << 28);
00070         return num % max;
00071         }
00072     private:
00073       TUnsigned8 rng8() {
00074         return (TUnsigned8) (Generator()*256);
00075         }
00076     };
00077   }
00078 
00079 
00080 
00081 
00082 #endif

Generated on Wed Oct 31 17:12:24 2001 for ixlib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001