Skip to content

Commit

Permalink
Merge branch 'master' of github.com:StefanoD/Rust_Random_Choice
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoD committed Aug 3, 2016
2 parents 020bcd0 + 94e8c3a commit 63979e4
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,33 @@ random_choice = "*"
```

## Examples
### In Place Variant
```rust
## Default Way
```
extern crate random_choice;
use self::random_choice::random_choice;
fn main() {
let mut samples = vec!["hi", "this", "is", "a", "test!"];
let weights: Vec<f64> = vec![5.6, 7.8, 9.7, 1.1, 2.0];
# fn main() {
let capacity: usize = 500;
let mut samples: Vec<usize> = Vec::with_capacity(capacity);
let mut weights: Vec<f64> = Vec::with_capacity(capacity);
random_choice().random_choice_in_place_f64(&mut samples, &weights);

for sample in samples {
print!("{}, ", sample);
}
for i in 0..capacity {
samples.push(i);
weights.push(i as f64);
}
```
### N Selection Variant
```rust
extern crate random_choice;
use self::random_choice::random_choice;
fn main() {
let mut samples = vec!["hi", "this", "is", "a", "test!"];
let weights: Vec<f64> = vec![5.6, 7.8, 9.7, 1.1, 2.0];
let number_choices = 10000;
let choices = random_choice().random_choice_f64(&samples, &weights, number_choices);
let number_choices = 100;
let choices = random_choice().random_choice_f64(&samples, &weights, number_choices);
assert!(choices.len() == number_choices);
for choice in choices {
print!("{}, ", choice);
}
for choice in choices {
print!("{}, ", choice);
}
# }
```
## With Custom Seed
```
### With Custom Seed
```rust
extern crate rand;
extern crate random_choice;
use random_choice::RandomChoice;
Expand All @@ -75,10 +67,11 @@ fn main() {
let rng = rand::StdRng::from_seed(&[5000, 44, 55, 199]);
let mut random_choice = RandomChoice::new(rng);
random_choice.random_choice_in_place_f64(&mut samples, &weights);
let number_choices = 100;
let choices = random_choice.random_choice_f64(&mut samples, &weights, number_choices);
for sample in samples {
print!("{}, ", sample);
for choice in choices {
print!("{}, ", choice);
}
}
```

0 comments on commit 63979e4

Please sign in to comment.