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 
00021 
00022 
00023 
00024 namespace ixion {
00025   class float_random {
00026     double Seed;
00027   
00028     public:
00029     float_random()
00030       : Seed(1)
00031       { }
00032   
00033     void init() { 
00034       time_t current_time = time(NULL);
00035       Seed = double(current_time)*sin(current_time); 
00036       }
00037     void init(double seed)
00038       { Seed = fabs(seed); }
00039 
00041     double operator()(double max = 1) {
00042       // normalize
00043       while (Seed > 3) Seed = log(Seed);
00044       Seed -= floor(Seed);
00045       Seed = pow(Seed+Pi,8);
00046       Seed -= floor(Seed);
00047       return Seed*max;
00048       }
00049     };
00050   
00051   
00052   
00053   
00054   class int_random {
00055       float_random      Generator;
00056   
00057     public:
00058       int_random()
00059         { }
00060     
00061       void init()
00062         { Generator.init(); }
00063       void init(unsigned seed)
00064         { Generator.init(seed); }
00065       
00067       unsigned operator()(unsigned max = 32768) {
00068         unsigned num = rng8() + (rng8() << 7) + (rng8() << 14) + (rng8() << 21) + (rng8() << 28);
00069         return num % max;
00070         }
00071     private:
00072       TUnsigned8 rng8() {
00073         return (TUnsigned8) (Generator()*256);
00074         }
00075     };
00076   }
00077 
00078 
00079 
00080 
00081 #endif

Generated at Tue Apr 17 18:29:43 2001 for ixlib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001