in reply to Choosing a random product from an Array
my @first = qw( 1 3 5 7 9 11 13 15 17 19 21 23 ); my @second = qw( 1 5 9 13 17 21 ); sub random_from { my ($first, $second) = @_; my %valid = ((map { $_ => 1 } @$first), (map { $_ => 0 } @$second)); my $return; do { # Choose a random key from the combined listing... $return = (keys %valid)[rand(keys %valid)]; # ...and make sure it's valid. } while (!$valid{$return}); return $return; } print "Selection: ",random_from(\@first, \@second),$/;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Choosing a random product from an Array
by sauoq (Abbot) on Nov 12, 2002 at 23:54 UTC | |
by tadman (Prior) on Nov 13, 2002 at 00:14 UTC | |
by sauoq (Abbot) on Nov 17, 2002 at 02:47 UTC |