BASE: Fix genrand to generate both 0s and 1s when max equals one.
authorAli Saidi <Ali.Saidi@ARM.com>
Mon, 23 Aug 2010 16:18:39 +0000 (11:18 -0500)
committerAli Saidi <Ali.Saidi@ARM.com>
Mon, 23 Aug 2010 16:18:39 +0000 (11:18 -0500)
previously was only generating 0s.

src/base/random.cc

index 7daa90b9cf98ed22757cc0a153dc239790c5d341..457b0c98be3115cfc6d17e669ae012742c1c34f6 100644 (file)
@@ -65,7 +65,9 @@ Random::~Random()
 uint32_t
 Random::genrand(uint32_t max)
 {
-    int log = ceilLog2(max);
+    if (max == 0)
+        return 0;
+    int log = ceilLog2(max) + 1;
     int shift = (sizeof(uint32_t) * 8 - log);
     uint32_t random;
 
@@ -79,7 +81,9 @@ Random::genrand(uint32_t max)
 uint64_t
 Random::genrand(uint64_t max)
 {
-    int log = ceilLog2(max);
+    if (max == 0)
+        return 0;
+    int log = ceilLog2(max) + 1;
     int shift = (sizeof(uint64_t) * 8 - log);
     uint64_t random;