I am trying to select a random number from two vectors, but they need to have matching indices. For example, if a random number in vs
vector is 2, then the number selected from vd
vector needs to be 7. Thanks!
extern crate rand;
use rand::Rng;
fn main() {
let vs = vec![0, 1, 2, 3, 4];
let vd = vec![5, 6, 7, 8, 9];
let mut ww = Vec::with_capacity(2);
let mut qq = Vec::with_capacity(2);
let sample_num = 2;
let mut num_sam = 0;
while sample_num > num_sam {
let mut ff = rand::thread_rng().choose(&vs);
let mut cc = rand::thread_rng().choose(&vd);
let mut index = vs.iter().position(|&r| r == ff).unwrap();
let mut index1 = vd.iter().position(|&r| r == cc).unwrap();
if index == index1 {
ww.push(ff);
qq.push(cc);
}
num_sam += 1;
}
println!("{:?}, {:?}", ww, qq)
}
Aucun commentaire:
Enregistrer un commentaire