Struct Picker

Source
pub struct Picker<T: Clone + Eq + Hash, R: RngCore> { /* private fields */ }
Expand description

Generator of groups of random items of type T with different probabilities. According to the configuration, items in each group can be either repetitive or non-repetitive.

Implementations§

Source§

impl<T: Clone + Eq + Hash> Picker<T, OsRng>

Source

pub fn build(conf: Config<T>) -> Result<Self, Error>

Builds the Picker with given configuration, using the OS random source.

Source§

impl<T: Clone + Eq + Hash, R: RngCore> Picker<T, R>

Source

pub fn build_with_rng(conf: Config<T>, rng: R) -> Result<Self, Error>

Builds the Picker with given configuration and the given random source.

Source

pub fn configure(&mut self, conf: Config<T>) -> Result<(), Error>

Applies new configuration.

Source

pub fn table_len(&self) -> usize

Returns the size of the weight table that contains all possible choices (p > 0).

use random_picker::Picker;
let mut conf: random_picker::Config<String> = "
    a = 0; b = 1; c = 1.1
".parse().unwrap();
let picker = Picker::build(conf.clone()).unwrap();
assert_eq!(picker.table_len(), 2);
conf.append_str("b = 0; c = 0");
assert!(Picker::build(conf).is_err());
Source

pub fn pick(&mut self, amount: usize) -> Result<Vec<T>, Error>

Picks amount of items and returns the group of items. amount must not exceed table_len().

Source

pub fn write_to(&mut self, dest: &mut [T]) -> Result<(), Error>

Picks dest.len() of items and writes them into dest (avoids allocation). Length of dest must not exceed table_len().

Source

pub fn test_freqs( &mut self, amount: usize, test_times: usize, ) -> Result<Table<T>, Error>

Evaluates probabilities of existences of table items in each group of length amount, by generating groups of items for test_times.

use random_picker::*;
let mut conf: Config<String> = "
    a=856; b=139; c=297; d=378; e=1304;
    f=289; g=199; h=528; i=627; j=  13;
    k= 42; l=339; m=249; n=707; o= 797;
    p=199; q= 12; r=677; s=607; t=1045;
    u=249; v= 92; w=149; x= 17; y= 199; z=8;
".parse().unwrap();
assert_eq!(conf.repetitive, false);
assert_eq!(conf.table.len(), 26);
let table_probs = conf.calc_probabilities(3).unwrap();

let mut picker = Picker::build(conf.clone()).unwrap();
let table_freqs = picker.test_freqs(3, 1_000_000).unwrap();
for (k, v) in table_freqs.iter() {
    assert!((*v - *table_probs.get(k).unwrap()).abs() < 0.005);
}

conf.append_str("repetitive = true");
assert_eq!(conf.repetitive, true);
let table_probs = conf.calc_probabilities(3).unwrap();;

let mut picker = Picker::build_with_rng(conf, rand::thread_rng()).unwrap();
let table_freqs = picker.test_freqs(3, 1_000_000).unwrap();
for (k, v) in table_freqs.iter() {
    assert!((*v - *table_probs.get(k).unwrap()).abs() < 0.005);
}

Auto Trait Implementations§

§

impl<T, R> Freeze for Picker<T, R>
where R: Freeze,

§

impl<T, R> RefUnwindSafe for Picker<T, R>

§

impl<T, R> Send for Picker<T, R>
where R: Send, T: Send,

§

impl<T, R> Sync for Picker<T, R>
where R: Sync, T: Sync,

§

impl<T, R> Unpin for Picker<T, R>
where R: Unpin, T: Unpin,

§

impl<T, R> UnwindSafe for Picker<T, R>
where R: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V