# Struct rand::distributions::WeightedChoice [−] [src]

    pub struct WeightedChoice<'a, T: 'a> {
        // some fields omitted
    }

A distribution that selects from a finite collection of weighted items.

Each item has an associated weight that influences how likely it is to be chosen: higher weight is more likely.

The `Clone` restriction is a limitation of the `Sample` and `IndependentSample` traits. Note that `&T` is (cheaply) `Clone` for all `T`, as is `u32`, so one can store references or indices into another vector.

# Example

    use rand::distributions::{Weighted, WeightedChoice, IndependentSample};

    let mut items = vec!(Weighted { weight: 2, item: 'a' },
                         Weighted { weight: 4, item: 'b' },
                         Weighted { weight: 1, item: 'c' });
    let wc = WeightedChoice::new(&mut items);
    let mut rng = rand::thread_rng();
    for _ in 0..16 {
         // on average prints 'a' 4 times, 'b' 8 and 'c' twice.
         println!("{}", wc.ind_sample(&mut rng));
    }

## Methods

### `impl<'a, T: Clone> WeightedChoice<'a, T>`

#### `fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T>`

Create a new `WeightedChoice`.

Panics if: - `v` is empty - the total weight is 0 - the total weight is larger than a `u32` can contain.

## Trait Implementations

### `impl<'a, T: Clone> Sample<T> for WeightedChoice<'a, T>`

#### `fn sample<R: Rng>(&mut self, rng: &mut R) -> T`

### `impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T>`

#### `fn ind_sample<R: Rng>(&self, rng: &mut R) -> T`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)