in reply to Need help with number generator
If you aren't worried about space, then that approach is fine enough, but perhaps:
Is more(?) clear?@a = split /\s+/, join " ", ("1 " x 8, "2 " x 1, "3 " x 1); print $a[int(rand 10)];
However, I would not approach it this way. Instead, I would do something like:
I think that will work, if not, you should at least get the general idea.my %wheel = {"One" => 8, "Two" => 1, "Three" => 1}; my $sum; my $item; foreach $item (keys %wheel) { $sum+=$wheel{$item}; } my $spin = int(rand($sum)); foreach $item (keys %wheel) { last if $sum < $wheel{$item}; $sum-=$wheel{$item}; } print "$item was picked\n";
G'Luck!
Gryn
|
|---|