Hi Zeus,
             Your code and explanation are good. Incase I want 10 non repeated random 
numbers between 0 and 1 what needs to be done? 

Regards
guruguhan

-----Original Message-----
From: Zeus Odin [mailto:[EMAIL PROTECTED]
Sent: Tuesday, July 27, 2004 5:17 AM
To: [EMAIL PROTECTED]
Subject: Re: [SOLUTION] how can i generate 10 unique (non repeating)
numbers


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";


"Absolut Newbie" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> greets to Peter Scott for pointing to Randal Schwartz's answer on C.L.P.M.
>
>
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=86hds1fa6n.fsf%40blue.stonehenge.com
>
> [SOLUTION]
> use List::Util from CPAN
> http://search.cpan.org/~gbarr/Scalar-List-Utils-1.14/lib/List/Util.pm
>
> basically you use the module to create a group of numbers (like a card
deck)
> and then "deal" them randomly into an array.



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



--
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