From: scipio Date: Fri, 26 Oct 2007 01:47:12 +0000 (+0000) Subject: Fix zero-seed bug: the random number generator generates a stream of X-Git-Tag: release_tinyos_2_1_0_0~682 X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=commitdiff_plain;h=1cf1bd75443cbe3cdca46118fbcd22843fc67b89;p=tinyos-2.x.git Fix zero-seed bug: the random number generator generates a stream of zeroes if initialized with 0. Thanks to Konrad Iwanicki for the catch. --- diff --git a/tos/lib/tossim/sim_tossim.c b/tos/lib/tossim/sim_tossim.c index 92df410f..d0d4d1f4 100644 --- a/tos/lib/tossim/sim_tossim.c +++ b/tos/lib/tossim/sim_tossim.c @@ -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; }