Randomizing a list
Author Message
Randomizing a list

Given a list of items.

How do i create a new list with a random order or
the initial list?

Thanks

Doron.

Wed, 24 Dec 2003 01:28:18 GMT
Randomizing a list

Quote:

>   Given a list of items.
>               How do i create a new list with a random order or
>              the initial list?

See the section "How do I shuffle an array randomly?" in perlfaq4.

Wed, 24 Dec 2003 00:51:36 GMT
Randomizing a list

Quote:

>   Given a list of items.

>               How do i create a new list with a random order or
>              the initial list?

#!/usr/bin/perl -w
use strict;

\$, = " ";

for(0..100){

Quote:
}

__END__

output :
before :  1 2 3 4 5 6 7 8 9
after :  5 6 3 7 8 9 1 4 2

The number of iterations necessary for a truly randomisation is
debatable.  I use 100 here as a proof of principle.

HTH

Paul

Wed, 24 Dec 2003 00:58:39 GMT
Randomizing a list

Quote:

>Snip post>
> The number of iterations necessary for a truly randomisation is
> debatable.  I use 100 here as a proof of principle.

Ahem.  Pardon my English...  I meant "for true randomisation".  It's
been a long day...  who would have thought that English was my first
language?

Sorry.

Paul

Wed, 24 Dec 2003 01:03:03 GMT
Randomizing a list

Quote:

>   Given a list of items.

>               How do i create a new list with a random order or
>              the initial list?

>               Thanks

>                 Doron.

Sounds like an FAQ:

perldoc -q random

Read for "How do I shuffle an array randomly?"

--
Tim Schmelter
Public Key available from http://www.keyserver.net
CAD7 2ABB 05A4 2F00 4CAE 7D2F 127C 129A 7173 2951

Wed, 24 Dec 2003 01:16:56 GMT
Randomizing a list

Quote:
>The number of iterations necessary for a truly randomisation is
>debatable.

No, it isn't.  It's mathematical fact.

Quote:
>  I use 100 here as a proof of principle.

Even 100 is not enough for unbiased shuffling with this algorithm.
Unless the list has fewer than 3 elements, this algorithm always
produces a biased shuffle.

rd
(\$p{\$_})&6];\$p{\$_}=/ ^\$P/ix?\$P:close\$_}keys%p}p;p;p;p;p;map{\$p{\$_}=~/^[P.]/&&
close\$_}%p;wait until\$?;map{/^r/&&<\$_>}%p;\$_=\$d[\$q];sleep rand(2)if/\S/;print

Thu, 25 Dec 2003 01:53:01 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages