[Chicago-talk] Random first numbers

Andrew Moore crap at mooresystems.com
Mon May 17 17:11:36 PDT 2010


On Mon, May 17, 2010 at 4:25 PM, Alan Mead <amead2 at alanmead.org> wrote:
> 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/;
>  shuffle(@conditions);
>  $$state{'condition'} = $conditions[0];

Is it necessary to shuffle the entire array when you seem to only want
a random element?

$state->{condition} = $conditions[ rand @conditions ];

Perhaps it is, and the justification wasn't shown, but sometimes I do
needless work myself, so I thought I'd check.

Cheers,
-Andy


More information about the Chicago-talk mailing list