Random sampling: scanning and selecting in Rust

45 Views Asked by At

I'm trying to implement in Rust the scanning and selecting algorithm for random sampling. So i want to sample m items form an input sequence S (streaming model), assuming the length of the sequence is knows (n), considering each item once for all. Starting from the first item, I extract a Real number and I compare it with the probability to select an item S[j], say P(j)= (m-s)/(n-j+1), where s is the number of items already selected before S[j].

Given the pseudocode:

s = 0 // sampled items
for (j = 1; (j <=n ) && (s < m); j++ ) do
   p = Rand(0,1);
   if ( p < (m-s)/(n-j+1) ) then
       select S[j];
       s++;
   end if
end for

I tried to implement it in Rust and I always sample the last 2 items of the sequence:

use::rand::Rng;

fn main() {
    let x=[1,2,3,4,5]; // Sequence
    let n = x.len(); 

    let m = 2;         //Items I want to sample
    let mut s = 0;     //items already sampled
    
    let mut p: f32 = 0.0;
   
    for j in (0..n) {
        while s<m {break}
            p= rand::thread_rng().gen_range(0.00..1.00);
            println!("rand number is:{p}");
            let mut probability:f32 = ((m - s) / (n - j)) as f32;
            println!("alpha: {probability}");
            if p < probability {
                println!("The picked item is {:?}", x[j]);
                s+=1;
            }  
    }
    
}

and that's the output:

rand number is:0.31380904
alpha: 0
rand number is:0.07166028
alpha: 0
rand number is:0.4985305
alpha: 0
rand number is:0.68924797
alpha: 1
The picked item is 4
rand number is:0.8122835
alpha: 1
The picked item is 5
0

There are 0 best solutions below