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

comment on

( [id://3333]=superdoc: 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

$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


In reply to Re^6: Perl - Unique Numbers by johngg
in thread Perl - Unique Numbers by deelinux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found