go ahead... be a heretic | |
PerlMonks |
Re: Rolling a biased dieby ViceRaid (Chaplain) |
on Apr 12, 2002 at 10:26 UTC ( [id://158539]=note: print w/replies, xml ) | Need Help?? |
Hi This one's bugged me before, but I think you can do it with full accuracy by creating a hash where the keys represent the bounds between different probabilities. For speed, I'm checking the most likely results first in the second iteration:
I'm not sure if there's a neater way of doing it without having to iterate the hash twice; maybe not, as you need to know the aggregate value of all the values of the weighted die first. Update: yep, there is a way, as IO ably demonstrated above. I didn't quite see how his/her algorithm was intended. It's shorter, and faster (by about three times, by my Benchmark). I'll doze off again. //=\\
In Section
Seekers of Perl Wisdom
|
|