bearophile wrote:
I have taken a look at the docs for dice(), I don't like its name because isn't 
intuitive at all, but its usage is easy. The usage of the function I have 
suggested is a bit more higher level.

An possible alternative design for such function is to take in input an already 
sorted array of the weights (beside the iterable of the items), this may speed 
up this function a bit (it just needs to call the algorithm for bisect search, 
I presume).

FWIW, I've implemented this sort of thing before. In my implementation, it was called ProbabilisticChooser(T), and I could instantiate it either with a pair of parallel arrays or with a HashMap(T, double).

In my case, I didn't lump it in with my other random-number related code, because I had a set of other classes implementing the Chooser(T) interface. Some of them were random and some were deterministic, but they all provided the same "choose" function on top of a "choice strategy" implementation.


--benji

Reply via email to