Class template discrete_distribution

| Home | Libraries | People | FAQ | More

## Class template discrete_distribution

boost::compute::discrete_distribution — Produces random integers on the interval [0, n), where probability of each integer is given by the weight of the ith integer divided by the sum of all weights.

## Synopsis

    // In header: <boost/compute/random/discrete_distribution.hpp>

    template<typename IntType = uint_>
    class discrete_distribution {
    public:
      // types
      typedef IntType result_type;

      // construct/copy/destruct
      template<typename InputIterator>
        discrete_distribution(InputIterator, InputIterator);
      ~discrete_distribution();

      // public member functions
      result_type n() const;
      ::std::vector< double > probabilities() const;
      template<typename OutputIterator, typename Generator>
        void generate(OutputIterator, OutputIterator, Generator &,
                      command_queue &);
    };

## Description

The following example shows how to setup a discrete distribution to produce 0 and 1 with equal probability

    // initialize the default random engine
    boost::compute::default_random_engine engine(queue);

    // initialize weights
    int weights[] = {2, 2};

    // setup the discrete distribution to produce integers 0 and 1
    // with equal weights
    boost::compute::discrete_distribution<uint_> distribution(weights, weights+2);

    // generate the random values and store them to 'vec'
    distribution.generate(vec.begin(), vec.end(), engine, queue);

### `discrete_distribution` public construct/copy/destruct

1.     template<typename InputIterator>
         discrete_distribution(InputIterator first, InputIterator last);

   Creates a new discrete distribution with weights given by the range [`first`, `last`)

2.     ~discrete_distribution();

   Destroys the `discrete_distribution` object.

### `discrete_distribution` public member functions

1.     result_type n() const;

   Returns the value of n.

2.     ::std::vector< double > probabilities() const;

   Returns the probabilities.

3.     template<typename OutputIterator, typename Generator>
         void generate(OutputIterator first, OutputIterator last,
                       Generator & generator, command_queue & queue);

   Generates uniformily distributed integers and stores them to the range [`first`, `last`).