Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

random number

by huskey (Beadle)
on Dec 13, 2002 at 03:43 UTC ( #219491=perlquestion: print w/replies, xml ) Need Help??

huskey has asked for the wisdom of the Perl Monks concerning the following question:

What I need is to give a list of members a random number selected from an array of numbers. For example if there were 200 members in my list, each member would get a randomly selected number between 1 and 200, and once a member had that number it would be removed from the array of numbers, so that each member has a unique random number. Does that make sense? What would be the easiest way to do this?

Replies are listed 'Best First'.
Re: random number
by traveler (Parson) on Dec 13, 2002 at 08:30 UTC
    You can just shuffle the list. Use
    use Algorithm::Numerical::Shuffle qw /shuffle/; @shuffled = shuffle (1..100);
    or
    @shuffled = shuffle (3,7,19,1000,44);
    You can also shuffle a list in place.

    HTH, --traveler

Re: random number
by BrowserUk (Pope) on Dec 13, 2002 at 03:53 UTC

    Take a look at this earlier post Re: unique random numbers and the thread it's in. It should get you started.


    Examine what is said, not who speaks.

Re: random number
by insensate (Hermit) on Dec 13, 2002 at 04:08 UTC
    Here's a snippit that will initialize an array and select a number while the array still has elements...what you do with the element is up to you...
    use strict; my $max = 200; #number of elements for +array my @array = (0..$max-1); #initialize array while(@array !=0){ my $index = rand @array; #pick a random element o +f the existing array my $element = $array[$index]; #extract the element my @newarray = grep {$_ !=$element} @array; #remove the element @array = @newarray; #reset the array }
    Addendum: See BrowserUk's reply for a more tidy/efficient method of extracting the element from the array

      Given that you know the index of the item you want to remove from the array, it would be much more efficient to use splice instead of grep to remove the item from the array.

      #replace my @newarray = grep {$_ !=$element} @array; #remove the element #with splice @array, $index, 1; #remove item from array; # and remove this. No need as the array has been modified. # @array = @newarray; #reset the array

      Examine what is said, not who speaks.

        #splice is still O(n) $array[$index] = $array[-1]; pop @array;

      Shuffling the array as suggested by BrowserUk and traveler is the Right Way To Do It™ but if you insist on removing elements from the array, how about

      perl -le '@a=1..200; print while $_ = splice @a, rand @a, 1'
      ?

      -sauoq
      "My two cents aren't worth a dime.";
      
      thank you. it worked perfect.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://219491]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2020-10-22 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?