Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Rolling a biased die

by tomazos (Deacon)
on Apr 13, 2002 at 14:38 UTC ( #158780=note: print w/replies, xml ) Need Help??


in reply to Re: Rolling a biased die
in thread Rolling a biased die

How about precaching a hash called %biasbase like this?

my $sum = 0; $biasbase{$_} = ($sum += $bias{$_}) foreach (reverse keys %bias);

And then iterate like this:

while( my($k,$v) = each %bias and not defined $rand){ $rand = $k if rand($biasbase{$k}) < $v; }

Does that make sense? That way you can bail out early.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://158780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2019-10-13 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?