YAFZ has asked for the wisdom of the Perl Monks concerning the following question:
I'd be glad indeed if experienced Perl monks could review my code. Is there a BETTER way to write this simulation? A faster way, a more elegant (or maybe more obfuscated ;-) one?#!/usr/bin/perl -w use strict; my $x; my $i; my @my_bag; @my_bag = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $i = @my_bag + 1; while (@my_bag > 0) { $x = int(rand($i)); $x = $x > 0 ? $x - 1 : $x; print splice(@my_bag, $x, 1) . "\n"; $i--; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simulating Drawing From A Bag
by broquaint (Abbot) on Jun 12, 2003 at 09:08 UTC | |
by YAFZ (Pilgrim) on Jun 12, 2003 at 10:55 UTC | |
|
Re: Simulating Drawing From A Bag
by Skeeve (Parson) on Jun 12, 2003 at 09:05 UTC | |
by YAFZ (Pilgrim) on Jun 12, 2003 at 10:52 UTC | |
|
Re: Simulating Drawing From A Bag
by arthas (Hermit) on Jun 12, 2003 at 09:05 UTC | |
|
Re: Simulating Drawing From A Bag
by Lachesis (Friar) on Jun 12, 2003 at 09:14 UTC | |
|
Re: Simulating Drawing From A Bag
by bigj (Monk) on Jun 12, 2003 at 09:50 UTC | |
|
Re: Simulating Drawing From A Bag
by Bilbo (Pilgrim) on Jun 12, 2003 at 09:12 UTC | |
|
Re: Simulating Drawing From A Bag
by gmpassos (Priest) on Jun 13, 2003 at 06:04 UTC |