00001 #ifndef RANDOM_H_IS_INCLUDED 00002 #define RANDOM_H_IS_INCLUDED 00003 00004 /*! 00005 * \file random.H 00006 * \brief Contains the definition of the RandomGen class. 00007 * \ingroup group_MLIB 00008 * 00009 */ 00010 00011 namespace mlib { 00012 00013 /*! 00014 * \brief Random number generator class. 00015 * \ingroup group_MLIB 00016 * 00017 * Private to objects (a la meshes) who need their own source of random 00018 * variables. 00019 * 00020 */ 00021 class RandomGen 00022 { 00023 00024 public: 00025 00026 //! \name Constructors 00027 //@{ 00028 00029 //! \brief Default constructor. Creates a random number generator with 00030 //! a default seed (1). 00031 RandomGen() { srand(); } 00032 00033 //! \brief Constructor that creates a random number generator with the 00034 //! seed given in the argument. 00035 RandomGen(long i) { srand(i);} 00036 00037 //@} 00038 00039 //! \brief The maximum integer value that can be produced by this random 00040 //! number generator. 00041 static long R_MAX; 00042 00043 //! \name Seed Accessor Functions 00044 //@{ 00045 00046 //! \brief Seeds the random number generator with the seed \p i. 00047 void srand(long i=1) { _seed = i ; } 00048 00049 //! \brief Gets the current seed value. 00050 //! 00051 //! \note This changes each time a new number is generated. 00052 long get_seed() { return _seed; } 00053 00054 //@} 00055 00056 //! \name Random Number Generation Functions 00057 //@{ 00058 00059 //! \brief Generate a random long integer. 00060 long lrand() { update(); return _seed; } 00061 00062 //! \brief Generate a random double precision floating point value in the 00063 //! range [0.0, 1.0]. 00064 double drand() { return (double) lrand()/(double)R_MAX ; } 00065 00066 //@} 00067 00068 private: 00069 00070 long _seed; //!< The current seed value. 00071 00072 //! \brief Generate the next seed (i.e. the next random value). 00073 void update(); 00074 00075 }; 00076 00077 } // namespace mlib 00078 00079 #endif // RANDOM_H_IS_INCLUDED