[Avida-cvs] [avida-svn] r1022 - branches/coopcomm/source/tools

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Sat Sep 30 20:52:23 PDT 2006


Author: dknoester
Date: 2006-09-30 23:52:22 -0400 (Sat, 30 Sep 2006)
New Revision: 1022

Modified:
   branches/coopcomm/source/tools/cRandom.h
Log:
This commit fixes an RNG casting error, cleans up the RNG a little, and adds a few asserts on parameters.



Modified: branches/coopcomm/source/tools/cRandom.h
===================================================================
--- branches/coopcomm/source/tools/cRandom.h	2006-09-30 01:52:22 UTC (rev 1021)
+++ branches/coopcomm/source/tools/cRandom.h	2006-10-01 03:52:22 UTC (rev 1022)
@@ -21,6 +21,7 @@
 #include <limits.h>
 #include <math.h>
 #include <pthread.h>
+#include <assert.h>
 
 /**
  * A versatile and fast pseudo random number generator.
@@ -101,7 +102,10 @@
    *
    * @return The pseudo random number.
    **/
-  double GetDouble() { return Get() * _RAND_FAC; }
+  inline double GetDouble()
+  {
+    return Get() * _RAND_FAC;
+  }
   
   /**
    * Generate a double between 0 and a given number.
@@ -109,7 +113,11 @@
    * @return The pseudo random number.
    * @param max The upper bound for the random numbers (will never be returned).
    **/
-  double GetDouble(const double max) { return GetDouble() * max; }
+  inline double GetDouble(const double max) 
+  {
+    assert(max>0.0);
+    return GetDouble() * max;
+  }
   
   /**
    * Generate a double out of a given interval.
@@ -118,7 +126,12 @@
    * @param min The lower bound for the random numbers.
    * @param max The upper bound for the random numbers (will never be returned).
    **/
-  double GetDouble(const double min, const double max) { return GetDouble() * (max - min) + min; }
+  inline double GetDouble(const double min, const double max) 
+  {
+    assert(max>min);
+    assert(max>0.0 && min>=0.0);
+    return GetDouble(max - min) + min;
+  }
   
   /**
    * Generate an unsigned int.
@@ -126,7 +139,10 @@
    * @return The pseudo random number.
    * @param max The upper bound for the random numbers (will never be returned).
    **/
-  unsigned int GetUInt(const unsigned int max) { return (int) (GetDouble() * max); }
+  inline unsigned int GetUInt(const unsigned int max)
+  {
+    return (unsigned int)GetDouble(max);
+  }
   
   /**
    * Generate an unsigned int out of an interval.
@@ -135,7 +151,10 @@
    * @param min The lower bound for the random numbers.
    * @param max The upper bound for the random numbers (will never be returned).
      **/
-  unsigned int GetUInt(const unsigned int min, const unsigned int max) { return GetUInt(max - min) + min; }
+  inline unsigned int GetUInt(const unsigned int min, const unsigned int max) 
+  {
+    return (unsigned int)GetDouble(min, max);
+  }
   
   /**
    * Generate an int out of an interval.
@@ -144,10 +163,17 @@
    * @param min The lower bound for the random numbers.
    * @param max The upper bound for the random numbers (will never be returned).
    **/
-  int GetInt(const int max) { return (int)GetUInt(max); }
-  int GetInt(const int min, const int max) { return ((int)GetUInt(max - min)) + min; }
+  inline int GetInt(const int max)
+  {
+    return (int)GetDouble(max);
+  }
   
+  inline int GetInt(const int min, const int max)
+  {
+    return (int)GetDouble(min, max);
+  }
   
+  
   // Random Event Generation //////////////////////////////////////////////////
   
   // P(p) => if p < [0,1) random variable




More information about the Avida-cvs mailing list