2 releases
0.1.1 | Apr 8, 2019 |
---|---|
0.1.0 | Apr 8, 2019 |
#325 in #vec
16KB
260 lines
weighted_random_list
A Vec that allows you to define the weight of each entry and randomly get entries:
extern crate weighted_random_list;
use weighted_random_list::WeightedRandomList;
fn main() {
let list = [
(1, "https://source.example.net/archive"),
(10, "https://mirror-slow0.example.net/archive"),
(10, "https://mirror-slow1.example.net/archive"),
(100, "https://mirror-fast.example.net/archive"),
];
let mirrors = list.iter()
.map(|(weight, url)| (*weight, url.to_string()))
.collect::<WeightedRandomList<String>>();
let random_choice = mirrors.get_random();
println!("Using {:?} this time", random_choice);
}
lib.rs
:
WeightedRandomList
enables you to to randomly select elements from
your collection while
Example select mirror
In this example we would like to give more traffic/weight to the the system "mirror-fast", less to the slow mirrors and the least to the main archive:
extern crate weighted_random_list;
use weighted_random_list::WeightedRandomList;
fn main() {
let list = [
(1, "https://source.example.net/archive"),
(10, "https://mirror-slow0.example.net/archive"),
(10, "https://mirror-slow1.example.net/archive"),
(100, "https://mirror-fast.example.net/archive"),
];
let mirrors = list.iter()
.map(|(weight, url)| (*weight, url.to_string()))
.collect::<WeightedRandomList<String>>();
let random_choice = mirrors.get_random();
println!("Using {:?} this time", random_choice);
}
Dependencies
~580–800KB
~11K SLoC