Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Random Derangement Of An Array

by jacques (Priest)
on Jul 06, 2008 at 03:47 UTC ( #695780=note: print w/ replies, xml ) Need Help??


in reply to Random Derangement Of An Array

sub fisher_yates_shuffle { my $deck = shift; # $deck is a reference to an array my $i = @$deck; while ($i--) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } } my @list = (1..5); fisher_yates_shuffle( \@list ); print join ', ',@list;


Comment on Re: Random Derangement Of An Array
Download Code
Replies are listed 'Best First'.
Re^2: Random Derangement Of An Array
by Limbic~Region (Chancellor) on Jul 06, 2008 at 04:12 UTC
    jacques,
    Is this a modified Fisher-Yates shuffle? I am too tired to tell. If it isn't, then it won't work because a shuffle is not guaranteed to generate a derangement. If it is - please explain the modification.

    Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2015-07-07 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls