in reply to Non-deterministic Testing

Force the starting seed (srand) during the unit tests.

- tye        

Replies are listed 'Best First'.
Re^2: Non-deterministic Testing (srand)
by itub (Priest) on Jan 28, 2005 at 17:26 UTC
    Caveat: this will make the tests reproducible only as long as you keep using the same platform (or perhaps even perl version, or compilation option). For example, the following program
    perl -le "srand(0); print rand"
    gives me the following results:
    0.00115966796875   v5.8.4 built for MSWin32-x86-multi-thread
    0.17082803610629   v5.8.0 built for i686-linux
    
      And of course we can fix this with the following code:
      BEGIN { my @list = 1..100; #or whatever; *CORE::GLOBAL::rand = sub { return shift @list } #or something }
      and now your rand will return what ever you want it to.
Re^2: Non-deterministic Testing (srand)
by Anonymous Monk on Jan 31, 2005 at 10:55 UTC
    But then you aren't testing the randomness of your function anymore. The OP wants to test whether about 20% of the tests return unpredictable results. By setting the seed of srand, or supplying the return values of rand as others suggest, you're no longer testing what you should be testing.