Problems? Is your data what you think it is? | |
PerlMonks |
Re^6: Perl - Unique Numbersby johngg (Canon) |
on Oct 18, 2015 at 22:46 UTC ( [id://1145274]=note: print w/replies, xml ) | Need Help?? |
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
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.
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: 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
In Section
Seekers of Perl Wisdom
|
|