www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Faster weighted choice range

reply bearophile <bearophileHUGS lycos.com> writes:
Along the time time I have put in Bugzilla a good amount of code for Phobos.

In Bugzilla I have an enhancement request that asks to turn std.random.dice
into a range:
http://d.puremagic.com/issues/show_bug.cgi?id=5849

Through Reddit I have found an efficient O(1) weighted choice:
http://www.keithschwarz.com/darts-dice-coins/

So I have translated the Java code, with small changes, into a D almost-range:
http://codepad.org/Ipp6R3DR
The code contains some questions, some TODOs, and probably a Range mistake too.

Bye,
bearophile
Dec 28 2011
parent bearophile <bearophileHUGS lycos.com> writes:
 So I have translated the Java code, with small changes, into a D almost-range:
 http://codepad.org/...
 The code contains some questions, some TODOs, and probably a Range mistake too.
Now it's a more correct Range: http://codepad.org/1TW3zp30 Bye, bearophile
Dec 29 2011