Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^6: Perl - Unique Numbers

by johngg (Canon)
on Oct 18, 2015 at 22:46 UTC ( [id://1145274]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Perl - Unique Numbers
in thread Perl - Unique Numbers

Where do you see that he saves anything?

I'm not sure what you are asking there. I have the %numbers hash, exactly equivalent to your %NoDups, in which I save the random numbers. This statement

$numbers{ int( rand 20 ) + 1 } ++

generates a random integer between 1 and 20 inclusive which is used as a key into the %numbers hash, the value associated with that key being incremented.

His loop handling is definitely non-Perl.
while keys %numbers < 10;

Nonsense! The while statement modifier is a standard Perl looping construct and will repeat the $numbers{ int( rand 20 ) + 1 } ++ statement until there are ten key/value pairs in %numbers, the keys being the unique random numbers.

He could just have done:
for ( 1..10 )

No, that would only yield ten numbers if no random number was repeated during the ten iterations. If there was one repeat there would only be nine numbers, only eight if two repeats etc.

Could you explain further why you think the code I posted is incorrect as I don't quite understand your objections.

Update: Added comment regarding use of for ( 1..10 ).

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2024-04-23 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found