[Chicago-talk] Random first numbers

Alan Mead amead2 at alanmead.org
Mon May 17 14:25:10 PDT 2010

I have a CGI-based study coded in Perl that needs to randomly assign 
participants to conditions.  The code below sucks:

  my @conditions = qw/cond1 cond2 cond3 cond4a/;
  $$state{'condition'} = $conditions[0];

I've just run through a dozen tests and cond3 appeared 90% of the time 
and cond2 10% of the time (cond1 and cond4a never appearing).  I haven't 
run the numbers, but I think this is "statistically significant". I 
think my shuffle is a "good" one from the on-line Perl lore, but maybe 
I'm wrong:

# Durstenfeld's implementation of Fisher and Yates
sub shuffle {
  my $n = scalar @_;
  while ( $n > 0 ) {
    my $j = int rand $n;
    ($_[$j], $_[$n]) = ($_[$n], $_[$j]); # swap

Assuming that shuffle() is not the culprit, I think it's just that when 
I run this code quickly/repeatedly, rand() is getting seeded with 
substantially the same value and is generating essentially the same 
initial value.  If so, my question is whether there is a better way to 
generate random numbers (so that the first values are more random). 

I was wondering if anyone agreed/disagreed and whether anyone had a 
"lazy" solution... if not, I can enforce better randomness by keeping 
some sort of state in a database.


Alan D. Mead, Ph.D.
Assistant Professor of Industrial and Organization Psychology
Scientific Advisor to the Center for Research and Service
Illinois Institute of Technology
3101 South Dearborn, 2nd floor
Chicago IL 60616
+312.567.5933 (Campus)
+815.588.3846 (Home Office)
+312.567.3493 (Fax)


He who fights with monsters should be careful lest he
thereby become a monster.
-- Friedrich Nietzsche, "Beyond Good and Evil"

More information about the Chicago-talk mailing list