swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Java randomization: How to drive yourself crazy in one easy step


From: Paul E Johnson
Subject: Java randomization: How to drive yourself crazy in one easy step
Date: Sat, 03 Mar 2001 11:02:46 -0600

I tweaked a program, and noticed the result changed, when I didn't think
it should. I tweaked it over and over, and kept finding differences
sometimes, not others.  Then I ran it over and over and found
differences sometimes, not others.  What the hell? I tightened down all
random number draws, to assure their seeds were constants.  Still
numbers varied. Dang. I lost most of my hair on it.

Then I realized I'd used a Java collections shuffler, and it is not
seeded with the rest of the swarm random numbers, and unlike the Swarm
philosophy, it does not use the same number stream each time. Here is
the culprit, in all its glory.

Collections.shuffle(recruiterList);

Stopping that shuffling brought the results into line, the same every
time.

Moral of the story?  The JDK's sources of randomness do not obey the
same logic as the randomness of Swarm itself.  If you want to use
Collections to shuffle a java collection, create a random number
generator, seed it, then use that in your shuffle command.  Something
like

Random newGenerator = new Random (seed);
Collections.shuffle(recruiterList,newGenerator);

Or use Swarm's randomized collection-traversal in scheduling. That will
protect you too.
-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]