Pathologically Eclectic Rubbish Lister PerlMonks

### Re: Re: Bingo Challenge

by CharlesClarkson (Curate)
 on May 20, 2002 at 02:56 UTC ( #167761=note: print w/replies, xml ) Need Help??

in reply to Re: Bingo Challenge

You could speed things by shuffling @words right before the two inner loops.

```foreach my \$i (1..\$cards) {
my %seen;
.
.
.
for my \$x (1..4) {
for my \$y (1..4) {
my \$word;
while (1) {
my \$index = rand(@words);
\$word = \$words[\$index];
if (!\$seen{\$word}) {
\$seen{\$word}++;
last;
}
}
.
.
.
becomes:
```use Algorithm::Numerical::Shuffle qw /shuffle/;
.
.
.
foreach my \$i (1..\$cards) {
.
.
.
@words = shuffle @words;
for my \$x (1..4) {
for my \$y (1..4) {
my \$word = shift @words;
.
.
.

HTH,
Charles K. Clarkson
Clarkson Energy Homes, Inc.

Replies are listed 'Best First'.
Re: Re: Re: Bingo Challenge
by Matts (Deacon) on May 20, 2002 at 06:47 UTC
Have you benchmarked that to see if it's faster?

My suspicion would be that for large data sets it would be, but for smaller ones my naive rand() implementation would be quickest.

Have you benchmarked that to see if it's faster?

No, I used logic. (which is why I said it could be faster.) A shuffling algorithm takes the same time to shuffle on each pass. It's really just randomizing the array indexes. The idiom you were using varies each time through. The less words left, the longer it will take to produce an unchosen word. My thought was, perhaps you were unaware of shuffling or its application here.

HTH,
Charles K. Clarkson
Clarkson Energy Homes, Inc.

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-05-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
If programming languages were movie genres, Perl would be:

Results (142 votes). Check out past polls.

Notices?