00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Jeff Lait 00008 * Side Effects Software Inc. 00009 * 477 Richmond Street West, Suite 1001 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 */ 00014 00015 #ifndef __SIM_Random_h__ 00016 #define __SIM_Random_h__ 00017 00018 #include "SIM_API.h" 00019 #include "SIM_DataUtils.h" 00020 00021 /// This class defines a bunch of useful tools for random number 00022 /// generation. It is designed to be attached as subdata to objects 00023 /// or solvers that need a stream of random numbers. 00024 class SIM_API SIM_Random : public SIM_Data 00025 { 00026 public: 00027 // Methods to manipulate the stream: 00028 00029 /// Sets the seed to the system. 00030 void setSeed(uint seed); 00031 00032 /// Gets a random unsigned integer 00033 uint urandom(); 00034 00035 /// Returns a random float from [0..1) 00036 fpreal frandom(); 00037 00038 /// Returns a random float from [-.5...5) 00039 fpreal frandom0(); 00040 00041 /// Returns a random integer from [0..choices-1] 00042 /// 0 choices will return 0. Negative choices will go 00043 /// for [-choices+1..0] 00044 int choice(int choices); 00045 00046 /// Returns a number within the range [a..b] 00047 /// In the floating point case, the range is [a..b) 00048 int irange(int a, int b); 00049 fpreal frange(fpreal a, fpreal b); 00050 00051 /// Returns true with the givent probability. < 0 always false, 00052 /// > 1 always true. 00053 bool chance(fpreal probability); 00054 00055 protected: 00056 explicit SIM_Random(const SIM_DataFactory *factory); 00057 virtual ~SIM_Random(); 00058 00059 /// Override this function to initialize the random number generator. 00060 virtual void setSeedSubclass(uint seed) = 0; 00061 /// Override this function to implement a new random number generator. 00062 virtual uint urandomSubclass() = 0; 00063 00064 private: 00065 DECLARE_STANDARD_GETCASTTOTYPE(); 00066 DECLARE_CLASSNAME(SIM_Random, SIM_Data); 00067 }; 00068 00069 #endif
1.5.9