Zeus Odin wrote:
If you want to do it without the use of a module, you could

(1) add the 15 numbers to an array (the deck)
(2) choose a random number from the deck
(3) remove that random element and add it to another array (your hand)
(4) repeat until done (your hand has 10 numbers)

This sounds exactly like the solution below sans module:

#!/usr/bin/perl
use warnings;
use strict;

my @nums = 1 .. 15;
my @ten;

for (1 .. 10) {
  push @ten, splice @nums, int(rand @nums), 1;
}
print "@ten";

Hmm, I may be wrong, but I don't think this is as random as it could be. The problem is that every time you remove an element from the list, you shrink the sample size. So the first selection is more random that the next, and that selection more random than the next, etc.


A better approach is to perform a Fisher-Yates shuffle on the list and then pop N elements from the list. This is what List::Util does.

Randy.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to