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, R: RngCore> Picker<T, R>
impl<T: Clone + Eq + Hash, R: RngCore> Picker<T, R>
Sourcepub fn build_with_rng(conf: Config<T>, rng: R) -> Result<Self, Error>
pub fn build_with_rng(conf: Config<T>, rng: R) -> Result<Self, Error>
Builds the Picker
with given configuration and the given random source.
Sourcepub fn table_len(&self) -> usize
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());
Sourcepub fn pick(&mut self, amount: usize) -> Result<Vec<T>, Error>
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()
.
Sourcepub fn write_to(&mut self, dest: &mut [T]) -> Result<(), Error>
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()
.
Sourcepub fn test_freqs(
&mut self,
amount: usize,
test_times: usize,
) -> Result<Table<T>, Error>
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>where
R: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, R> Send for Picker<T, R>
impl<T, R> Sync for Picker<T, R>
impl<T, R> Unpin for Picker<T, R>
impl<T, R> UnwindSafe for Picker<T, R>where
R: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more