]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Fix zero-seed bug: the random number generator generates a stream of
authorscipio <scipio>
Fri, 26 Oct 2007 01:47:12 +0000 (01:47 +0000)
committerscipio <scipio>
Fri, 26 Oct 2007 01:47:12 +0000 (01:47 +0000)
zeroes if initialized with 0. Thanks to Konrad Iwanicki for the catch.

tos/lib/tossim/sim_tossim.c

index 92df410fe7a0b69ad5f849cd610c7a571591950f..d0d4d1f407e61a98246003c1464c70a985f17298 100644 (file)
@@ -55,7 +55,17 @@ void sim_init() __attribute__ ((C, spontaneous)) {
   {
     struct timeval tv;
     gettimeofday(&tv, NULL);
-    sim_seed = tv.tv_usec;
+    // Need to make sure we don't pass zero to seed simulation.
+    // But in case some weird timing factor causes usec to always
+    // be zero, default to tv_sec. Note that the explicit
+    // seeding call also has a check for zero. Thanks to Konrad
+    // Iwanicki for finding this. -pal
+    if (tv.tv_usec != 0) {
+      sim_random_seed(tv.tv_usec);
+    }
+    else {
+      sim_random_seed(tv.tv_sec);
+    }
   } 
 }
 
@@ -82,6 +92,10 @@ int sim_random() __attribute__ ((C, spontaneous)) {
 }
 
 void sim_random_seed(int seed) __attribute__ ((C, spontaneous)) {
+  // A seed of zero wedges on zero, so use 1 instead.
+  if (seed == 0) {
+    seed = 1;
+  }
   sim_seed = seed;
 }