Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Randomizing an array

by svsingh (Priest)
on Jun 15, 2003 at 08:14 UTC ( [id://266007]=note: print w/replies, xml ) Need Help??


in reply to Randomizing an array

This is similar to a shuffle routine I wrote in college. Basically, you swap two elements in the array at random. I used to do this n^2 times, but since you have so many elements, that may not be too effective for you. Instead I took a cue from cciulla's post and perform n swaps. For 170,000 elements, it didn't take too long to run on my end.
my @shuffledList = &shuffle(1..170000); sub shuffle { my @sl = @_; # soon to be Sorted List for my $i ( 1..$#sl ) { my $x = int(rand() * $#sl); my $y = int(rand() * $#sl); ($sl[$x], $sl[$y]) = ($sl[$y], $sl[$x]); } return @sl; }
Hope this helps.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found