samedi 27 août 2022

Exclude value out of Rand gen_range

I would like to have a random number between -max and max where max is some i32 value, but I would like to exclude zero. I am using rand version 0.8.5.

I would like to have a code something like

let mut rng = rand::thread_rng();
rng.gen_range(-max..max);

But this code has zero included. Is there an idiomatic way to exclude it?




Aucun commentaire:

Enregistrer un commentaire