Jump to content

One of n lines in a file: Difference between revisions

Line 1,916:
fn one_of_n<R: Rng>(rng: &mut R, n: usize) -> usize {
(1..n).fold(0, |keep, cand|
matchif rng.next_f64() < (1.0 / (cand + 1) as f64) {
y if y < (1.0 / (cand + 1) as f64) => cand,
} else _ => keep {
keep
}
)
Line 1,924 ⟶ 1,925:
 
fn main() {
letconst mutLINES: distusize = [0_usize; 10];
 
let mut dist = [0; LINES];
let mut rng = thread_rng();
 
for _ in 0..1_000_000 {
let num = one_of_n(&mut rng, 10LINES);
dist[num] += 1;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.