Boost logo

Boost Users :

From: Marshall Clow (marshall_at_[hidden])
Date: 2008-05-03 19:57:36


At 1:12 PM +0100 5/3/08, Kevin Martin wrote:
>I'm looking for a random distribution which allows me to generate
>enums with specified probabilities.
>
>As a simple example I could have:
>
>//The enum.
>typedef enum {vt_car, vt_bus, vt_walk, vt_cycle} vehicle_types;
>
>//What I want to generate and the associated probabilities.
>boost::array<vehicle_types, 4> items = {vt_car, vt_bus, vt_walk,
>vt_cycle};
>boost::array<double, 4> probabilities = {0.1, 0.2, 0.3, 0.4}

        This should be pretty easy to write. I would be tempted to
pass in a "const boost::array < std::pair<vehicle_type, double>, N>",
which would give return values and probabilities; and use a
std::vector<double> to calculate cumulative probabilities, and for
each call to the RNG, do a binary search in the vector to find the
right spot, and then return the associated value.

-- 
-- Marshall
Marshall Clow     Idio Software   <mailto:marshall_at_[hidden]>
It is by caffeine alone I set my mind in motion.
It is by the beans of Java that thoughts acquire speed,
the hands acquire shaking, the shaking becomes a warning.
It is by caffeine alone I set my mind in motion.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net