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

Re^4: Perl - Unique Numbers

by dsheroh (Monsignor)
on Oct 18, 2015 at 09:01 UTC ( [id://1145246]=note: print w/replies, xml ) Need Help??


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

Take a closer look at how his code works. It keeps generating numbers until it has 10 unique numbers, then prints out a list of those 10 unique numbers. Even if a number has been chosen several times, it still counts as only one unique number and will only be printed once. Thus, no duplicates will appear in the output.

The main difference between his code and yours is that, if a number is selected three times, yours will have a value of 1 in the hash for that key, while his will have a value of 3. But, since the values are never used for anything (you just output the keys and ignore the values), it doesn't matter either way.

Replies are listed 'Best First'.
Re^5: Perl - Unique Numbers
by flexvault (Monsignor) on Oct 18, 2015 at 15:28 UTC

    deelinux,

      Take a closer look at how his code works. It keeps generating numbers until it has 10 unique numbers, then prints out a list of those 10 unique numbers...

    No, his code prints 10 numbers. Where do you see that he saves anything?

    His loop handling is definitely non-Perl. He could just have done:
    for ( 1..10 )
    For such a trivial problem, if he had one previous response, I wouldn't have commented at all. When I posted, I was the 3rd comment, so I added the 'another way of doing' phrase.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      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

      Again, take a closer look at his code.

      His loop is not equivalent to for (1 .. 10). Rewriting his loop in a more "conventional" way, it's equivalent to:

      while (keys %numbers < 10) { my $num = int( rand 20 ) + 1; $numbers{$num}++; }
      It will loop until there are 10 keys in %numbers, which means that 10 distinct numbers have been chosen, since hashes can't have duplicate keys.

      Where do you see that he saves anything?

      Because the value associated with the key is incremented each time that value is chosen, the keys will "save" the list of distinct numbers chosen, while the values will "save" the number of times each one came up.

      His loop handling is definitely non-Perl.

      I think we'll just have to disagree on that one. I find

      $numbers{int(rand(20))+1}++ while keys %numbers < 10;
      to be far more idiomatically Perlish than the "conventional" equivalent I provided above.

      P.S. You seem to have gotten your attributions crossed. I'm not deelinux.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found