[Tinyos-devel] Random numbers in TOSSIM

Konrad Iwanicki iwanicki at few.vu.nl
Thu Oct 11 05:29:59 PDT 2007


Hi all,

TOSSIM has some drawbacks regarding random seed initialization.

1. It may happen that the seed is set to 0. As a result, all randomly
generated numbers are 0. This situation occurred a number of times when
I was extensively testing of my algorithms with TOSSIM, so it is not
unlikely to happen.

2. It is virtually impossible to repeat experiments with the same random
seed without modifying TOSSIM.

For these reasons, I made some small changes to the sim_init() function
in the sim_tossim.c file (see below). Essentially, one can redefine a
macro, TOSSIM_RANDOM_SEED, at compile time to set his/her own seed. In
addition, the code prevents the seed from being equal to 0.

**Can somebody integrate these changes with the sourceforge CVS tree**?


*CHANGES*: *the code in file* sim_tossim.c:

void sim_init() __attribute__ ((C, spontaneous)) {
   sim_queue_init();
   sim_log_init();
   sim_log_commit_change();
   sim_noise_init(); //added by HyungJune Lee

   {
     struct timeval tv;
     gettimeofday(&tv, NULL);
     sim_seed = tv.tv_usec;
   }
}


*becomes*:


void sim_init() __attribute__ ((C, spontaneous)) {
   sim_queue_init();
   sim_log_init();
   sim_log_commit_change();
   sim_noise_init(); //added by HyungJune Lee

   {
     // --- modified by Konrad Iwanicki
#ifdef TOSSIM_RANDOM_SEED
#if (TOSSIM_RANDOM_SEED != 0)
     sim_seed = TOSSIM_RANDOM_SEED;
#else
#warning TOSSIM_RANDOM_SEED cannot be 0. Changing it to 1.
     sim_seed = 1;
#endif
#else
     struct timeval tv;
     gettimeofday(&tv, NULL);
     sim_seed = tv.tv_usec;
     if (sim_seed == 0) {
         sim_seed = tv.tv_sec;
     }
#endif
     // --- end of modifications
   }
}


*END of changes*


Take care,
-- 
- Konrad Iwanicki.


More information about the Tinyos-devel mailing list