Rust question: How do I pick 2 different elements from a range? My code look like this:…
- User
Rust question:
How do I pick 2 different elements from a range?
My code look like this: let color0: u8 = rng.gen_range(0..=9); let mut color1: u8 = color0; while color1 == color0 { color1 = rng.gen_range(0..=9); }
Is there a nicer way
- Assistant
Yes, there's a cleaner and more efficient way to solve this by using
sample_iterandtakemethods from therandcrate.First, you need to use
Uniformto specify the range, then generate an iterator over random values within that range. You then use to get the desired number of unique values.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.