Randomising an array
tamaguchi
created: 2006-08-04 08:42:08
I would like to randomise and array in a easy way. What do you think of this one?
my @arr=(1..20);

for(1..200)
{
	$random_position=int(rand($#arr));
	$var=splice(@arr, $random_position, 1);
	push(@arr, $var);
}

foreach (@arr)
{print "$_\n";}
Re: Randomising an array
created: 2006-08-04 08:43:49
tamaguchi,
This is a FAQ but the short answer is to use List::Util's shuffle() function. If you are interested in other ways then search the FAQs ;-)

Cheers - L~R

Re: Randomising an array
created: 2006-08-04 08:57:16

Why don't people read FAQs any more?

How do I shuffle an array randomly?

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re^2: Randomising an array
created: 2006-08-04 09:41:22
Why don't people read FAQs any more?
Maybe because they don't know how? :)
perldoc -q random
or one could take a look in the Perl FAQ here at PM, sneakily called [id://1843] (array->how do I shuffle).

-- Hofmator

Re^3: Randomising an array
created: 2006-08-06 04:09:46
If you can ask here, you know how to search
Re: Randomising an array
created: 2006-08-04 09:00:47

Hi [tamaguchi], Try this,

  use strict;
  use warnings;
  use Data::Random qw(:all);
   
  my @set = (1..200);  
  my @random_set = rand_set( set => \@set, size => $#set );  
  $,="\n";  
  print @random_set;

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Randomising an array
created: 2006-08-04 10:27:25

You need to know this: The shuffle() routine implemented in List::Util is a fair shuffle; It is an implementation of the fisher-yates shuffle. Other recipies such as yours are not certain to be 'fair'. I don't know if yours is biased or not, but why leave it to chance when there's a perfectly good alternative built into a module that comes with Perl? I do know that it is very easy to get it wrong when writing an unbiased shuffle routine. Good thing someone included a fair, unbiased shuffle in List::Util


Dave

Re^2: Randomising an array
created: 2006-08-04 12:19:13
The OP's shuffle does have a bug. It should have rand scalar @arr instead of rand $#arr. But if that is fixed (and "20" and "200" are changed to match each other), then it is a fair shuffle. However the OP's approach is still inferior to Fisher-Yates in that it has quadratic instead of linear running time (because of the splicing), and it does not shuffle in place like F-Y.

blokhead

Re: Randomising an array
created: 2006-08-04 12:51:52

If this is for a class project or a nuclear power plant I wouldn't do it this way:

@a = (0..20);$,="\n";
for(0..200){$b=shift @a;push @a,(int rand 2)?($b,shift @a):(shift@a,$b)}
print @a;


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Randomising an array
created: 2006-08-04 17:21:42

I recommend a look at the node 1824 section of Categorized Questions and Answers:

Super Search is your friend:

... and so on.

HTH,

planetscape
Re: Randomising an array
created: 2006-08-05 23:08:58

The best way to unsort an array? Sort it!!

@a=0..20;$,=" ";
print sort {1-int rand 3} @a;

OK, I couldn't resist golfing it. Actually this is a really bad randomizer. It behaves more like an incomplete shuffle. It's also not guaranteed to finish but the failure should be extremely rare.

s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

perlmonks.org content © perlmonks.org and Anonymous Monk, blokhead, davido, davorg, Hofmator, Limbic~Region, planetscape, Samy_rio, starbolin, tamaguchi

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03