Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Trying to get sum of elements in array

by gargle (Chaplain)
on Feb 06, 2015 at 12:05 UTC ( [id://1115771]=note: print w/replies, xml ) Need Help??


in reply to Trying to get sum of elements in array

About filling up your array... you're using a random index number and will only stop if there are 5 different indexes, possibly looping more than 5 times. I find the following much more clean: It only loops 5 times, for sure:
my @list = (); foreach my $i (1..5) { push @list, int(rand(50)+1); }
--
if ( 1 ) { $postman->ring() for (1..2); }

Replies are listed 'Best First'.
Re^2: Trying to get sum of elements in array
by LanX (Saint) on Feb 06, 2015 at 12:38 UTC
    But the result isn't guaranteed to be unique.

    If you worry about performance rather use a shuffle approach.

    Or use the fact that the order of keys in a hash is random.

    Cheers Rolf

    PS: Je suis Charlie!

      Why do these random numbers have to be unique?
      --
      if ( 1 ) { $postman->ring() for (1..2); }
        We've already established that this is a homework assignment. Perhaps uniqueness is one of the assignment's constraints?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found