[SOLUTION] how can i generate 10 unique (non repeating) numbers

From: Absolut Newbie (dotted_i_at_vitaemail.com)
Date: 07/25/04


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


Relevant Pages

  • Re: Two candies
    ... xrange() and repeat() objects only take-up a few bytes. ... lists and xrange objects also support the sequence protocol (getitem ... to pre-size the array and it uses the getitem protocol to access the ...
    (comp.lang.python)
  • Re: recursive permutation
    ... > I tried to do it each time permute is called but its not working so what i ... If you're struggling with the size of your array, ... you to repeat that information. ... Chris Smith - Lead Software Developer/Technical Trainer ...
    (comp.lang.java.help)
  • Re: How would you do this in forth?
    ... I thought I'd just code up a quick array based solution before making ... You might try doing "found" with a DO LOOP instead of BEGIN WHILE REPEAT ... If I doesn't have to be on the stack then not-yet and got-it: ...
    (comp.lang.forth)
  • Re: [fitsbits] Interpretation of repeat count in binary tables
    ... There is no explicit relationship between the vectors in different rows of the table. ... FITS readers can legitimately interpret the same table in different ways? ... If there is no TDIMn keyword, then the vector would be interpreted as a 1024 element 1-D array. ... The point being that, I guess because some fits readers are highly inefficient at reading tables with short rows, it has become common practice at least in the CMB field to use a repeat count just as a way of packing data in a way that can be read efficiently. ...
    (sci.astro.fits)
  • Re: Random Subset of Range of Integers
    ... there is an Oalgorithm to generate a ... So, since a sebset is returned, the numbers CANNOT repeat and theren lies ... array or vector, but the PROBLEM with these is that the function does not ... that specify a range, giving the function a vector full of integers in the ...
    (comp.lang.cpp)