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 "[EMAIL PROTECTED] is --------> @shuffled\n";
@sorted = sort { $a <=> $b } @shuffled;
print "[EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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



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