I am exploring different metaheuristics, and I need to fix the seed for random number generation.
I used let mut rng = StdRng::seed_from_u64(seed), but my problem is when I pass around this var through different functions, because all of random methods I use need to have a mut reference to rng, thus I need to clone in some steps rng.
And when I clone, the random sequence rng produces it's repeated. I show a toy example:
let seed: u64 = 10;
let iterations = 10;
println!("Seed fixed to {}, printing {} values", seed, iterations);
let mut rng = StdRng::seed_from_u64(seed);
for _ in 0..iterations{
let rand_value = rng.gen_range(0..1000);
println!("Random value: {}", rand_value);
}
println!("");
println!("Original generator is cloned");
let mut other_rng = rng.clone();
for _ in 0..iterations{
let rand_value = other_rng.gen_range(0..1000);
println!("Random value: {}", rand_value);
}
println!("");
println!("Original generator is cloned (not the second generator)");
let mut yet_another_rng = rng.clone();
for _ in 0..iterations{
let rand_value = yet_another_rng.gen_range(0..1000);
println!("Random value: {}", rand_value);
}
Which outputs:
Seed fixed to 10, printing 10 values
Random value: 386
Random value: 130
Random value: 336
Random value: 182
Random value: 624
Random value: 356
Random value: 773
Random value: 110
Random value: 783
Random value: 485
Original generator is cloned
Random value: 693
Random value: 538
Random value: 242
Random value: 121
Random value: 258
Random value: 991
Random value: 633
Random value: 701
Random value: 831
Random value: 90
Original generator is cloned (not the second generator)
Random value: 693
Random value: 538
Random value: 242
Random value: 121
Random value: 258
Random value: 991
Random value: 633
Random value: 701
Random value: 831
Random value: 90
So is clear that second and third random sequence are the same because we are cloning the same original rng. I would like to have a mechanism to "clone" or wathever and change that state, not produce the exact same sequence. Something like we do in C/C++ at the start of main function
Any idea of how can I pass rng through different functions?