On Jan 24, 2004, at 3:31 PM, Charles Lu wrote:


$probability = {  '1' => 0.1,
                      '2' => 0.1,
                      '3' => 0.7,
                      '4 => 0.1
                    }

What if you did it the other way around?


        my $probability= {0 => 1, 1 => 2, 9 => 4};
        my $value = 10;
        
        for(my $i= 0 ; $i <$value ; $i++ )
        {
                $probability->{$i} = 3
                        unless(exists($probability->{$i}));
        }
        my $roll = int(rand($value));
        
        print " \$roll is => $roll = shows side $probability->{$roll} \n";

notice we will wind up with ten elements in the hash ref,
and we have already assigned our basics...

Or you could have gone with a list.

        my @probability = qw/1 2 3 3 3 3 3 3 3 4/;
        my $value = 10;
        my $roll = int(rand($value));
        print " \$roll is => $roll = shows side $probability[$roll] \n";

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to