good chemistry is complicated,and a little bit messy -LW PerlMonks

### Re: Curious find while comparing grep, map, and smart match...

by BrowserUk (Pope)
 on Mar 27, 2013 at 02:31 UTC ( #1025617=note: print w/replies, xml ) Need Help??

All of your methods of picking 100 random numbers between 1 & 120 are horribly complicated and inefficient:

```use List::Util qw[ shuffle ];

my @rands = ( shuffle 1 .. 120 )[ 0 .. 99 ];

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
poor man's shuffle :)
by LanX (Chancellor) on Mar 27, 2013 at 19:38 UTC
Just for fun, here a poor mans shuffle:

```  DB<228> sort { (-1)**(rand(2)%2)  } 1..10
=> (5, 8, 7, 6, 9, 10, 1, 4, 2, 3)

Surely neither efficient, nor well mixed and pretty pointless since List::Util is core. =)

Cheers Rolf

( addicted to the Perl Programming Language)

##### update

for completeness:

> nor well mixed

The distribution is perfect iff the list has 2^n elements

```  DB<483> for (1..100000) {\$x=0;\$g[\$x++]{\$_}++ for sort {(-1) ** int r
+and 2} a..d }
=> ""

DB<484> \@g
=> [
{ a => 25302, b => 25028, c => 24852, d => 24818 },
{ a => 25062, b => 24925, c => 24773, d => 25240 },
{ a => 24773, b => 24944, c => 25203, d => 25080 },
{ a => 24863, b => 25103, c => 25172, d => 24862 },
]
Surely neither efficient, nor well mixed and pretty pointless ...

Agreed.

> Agreed.

=)

Cheers Rolf

( addicted to the Perl Programming Language)

Re^2: Curious find while comparing grep, map, and smart match...
by dbuckhal (Pilgrim) on Mar 27, 2013 at 04:50 UTC
Explain.

I will make an effort to interpret BrowserUKs terse comment for you :-)

In grepGen/mapGen/smartGen you have a redo in the event of a collision, whereas if you really want unique random numbers, they could be generated much more simply by the shuffle method he suggested. His method does not involve repeated retries and removes the need for any of these functions. His one liner does what each of the three methods you proposed does, but much more efficiently....

```my @rands = (
shuffle       # shuffle, like a pack of cards
1 .. 120      # a series of numbers from 1..120
)
[ 0 .. 99 ];  # take the first 100 of these

# the resulting @rands is a 100 element array of numbers
# in the range 1..120, all of which are unique
A Monk aims to give answers to those who have none, and to learn from those who know more.

Thank you for your interpratation. :D I knew what he was trying to say, but in this instance he was wrong. I was measuring each of those functions against a pool of random numbers, where they would each encounter the same set, including duplicates. So, the script was written as I thought is should be to accomplish my goal.

I always enjoy peeking around here reading the insights presented to everyone's code, even such comments which may not be directly relevant, but target the improving of one's coding skills. But, sometimes a question of what is the goal of the program can be more educational for both sides.

As always, comments and opinions are appreciated.

Explain what?

The code I posted; or why your code is inefficient?

Create A New User
Node Status?
node history
Node Type: note [id://1025617]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-08-21 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (321 votes). Check out past polls.

Notices?