Keep It Simple, Stupid PerlMonks

### Re^5: Perl - Unique Numbers

by flexvault (Monsignor)
 on Oct 18, 2015 at 15:28 UTC ( #1145260=note: print w/replies, xml ) Need Help??

in reply to Re^4: 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...

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

Replies are listed 'Best First'.
Re^6: Perl - Unique Numbers
by johngg (Canon) on Oct 18, 2015 at 22:46 UTC
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

Re^6: Perl - Unique Numbers
by dsheroh (Monsignor) on Oct 19, 2015 at 12:06 UTC
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.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1145260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-01-17 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2022, my preferred method to securely store passwords is:

Results (51 votes). Check out past polls.

Notices?