develooper Front page | perl.beginners | Postings from April 2002

RE: Scripts picks random elements from array , but it repeats sometimes

Thread Previous
From:
Bob Showalter
Date:
April 29, 2002 12:18
Subject:
RE: Scripts picks random elements from array , but it repeats sometimes
Message ID:
2E4528861499D41199D200A0C9B15BC031B9D3@taylorwhite.com
> -----Original Message-----
> From: FLAHERTY, JIM-CONT [mailto:Jim.Flaherty@cnet.navy.mil]
> Sent: Monday, April 29, 2002 2:58 PM
> To: Beginners (E-mail)
> Subject: FW: Scripts picks random elements from array , but it repeats
> som etimes
> 
> 
>  
> Hello , My script picks out random elements from an array , 
> but it sometimes
> repeats ( which is no good) .. I want to prevent this , Do I 
> delete the
> element from the array .... or is there a better way ? And how do I do
> either ?  
>  
>  
>  
> my mistake here is the code 
>  
>  
>  
>  
> while($numofques > 0) {
>     $index   = rand @numbers;
>     $element = $numbers[$index];

Perhaps you want a shuffle; i.e. put the elements in random
order and then iterate through the list. Can't say without
more info on what you mean by "[repeating is] no good".

perldoc -q shuffle

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About