[SOLUTION] how can i generate 10 unique (non repeating) numbers
From: Absolut Newbie (dotted_i_at_vitaemail.com)
Date: 07/25/04
- Next message: Renzo Rizzato: "Trouble with PERL and HTML"
- Previous message: Nicolay A. Vasiliev: "Get .exe or .zip file from the server via perl script"
- Next in thread: Zeus Odin: "Re: [SOLUTION] how can i generate 10 unique (non repeating) numbers"
- Reply: Zeus Odin: "Re: [SOLUTION] how can i generate 10 unique (non repeating) numbers"
- Maybe reply: Guruguhan \ N: "RE: [SOLUTION] how can i generate 10 unique (non repeating) numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: beginners@perl.org Date: Sun, 25 Jul 2004 17:13:25 +0300
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.
use List::Util qw(shuffle);
@cards = shuffle 0..51; # 0..51 in a random order
so my final code looks like :
use List::Util qw(shuffle);
@shuffled = shuffle(1..10);
print "\@shuffled is --------> @shuffled\n";
@sorted = sort { $a <=> $b } @shuffled;
print "\@shuffled is now --------> @sorted\n";
[ORIGINAL QUESTION]
Hi,
I want to generate 10 numbers from 1..15 and put them in an array.
easy ?
while ($fill < 10){
$foo = int(rand(15));
unshift(@array, $foo);
$fill++;
}
print "the \@array is --> @array\n";
my problem is that many times the numbers repeat themselves in the array.
how can i generate 10 unique (non repeating) numbers from a range to put in
the array ?
thanx.
--- Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.725 / Virus Database: 480 - Release Date: 7/19/2004
- Next message: Renzo Rizzato: "Trouble with PERL and HTML"
- Previous message: Nicolay A. Vasiliev: "Get .exe or .zip file from the server via perl script"
- Next in thread: Zeus Odin: "Re: [SOLUTION] how can i generate 10 unique (non repeating) numbers"
- Reply: Zeus Odin: "Re: [SOLUTION] how can i generate 10 unique (non repeating) numbers"
- Maybe reply: Guruguhan \ N: "RE: [SOLUTION] how can i generate 10 unique (non repeating) numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|