Reputation: 4132
How do I generate a vector of 100 64-bit integer values in the range from 1 to 20, allowing duplicates?
Upvotes: 20
Views: 22904
Reputation: 444
use std::error::Error;
use std::hash::{BuildHasher, Hasher, RandomState};
fn main() -> Result<(), Box<dyn Error>> {
// Example: Get a random integer value in the range 1 to 20:
let value: u64 = get_random_integer(1, 20)?;
println!("integer: {:?}", value);
// Generate a vector of 100 64-bit integer values in the range from 1 to 20,
// allowing duplicates:
let integers: Vec<u64> = (0..100)
.map(|_| get_random_integer(1, 20))
.collect::<Result<Vec<u64>, _>>()?;
println!("integers: {:?}", integers);
Ok(())
}
Generate a random integer value in the given range (min, max), inclusive.
Return error if min > max
.
fn get_random_integer(min: u64, max: u64) -> Result<u64, Box<dyn Error>> {
if min > max {
Err(format!(
"Error: min ({}) must be less than or equal to max ({})",
min, max
)
.into())
} else {
// The remainder (`%`) after division is always less than the divisor.
Ok(min + rand() % (max - min + 1))
}
}
Generate random numbers without external dependencies
pub fn rand() -> u64 {
RandomState::new().build_hasher().finish()
}
See the Rust Playground
Result<u64, Box<dyn Error>>
If it is guaranteed that always min <= max
in the range (min, max), then the code can be simplified to:
use std::hash::{BuildHasher, Hasher, RandomState};
fn main() {
// Generate a vector of 100 64-bit integer values in the range from 1 to 20,
// allowing duplicates:
let integers: Vec<u64> = (0..100)
.map(|_| get_random_integer(1, 20))
.collect();
println!("integers: {:?}", integers);
}
/// Generate a random integer value in the given range (min, max) inclusive.
fn get_random_integer(min: u64, max: u64) -> u64 {
min + rand() % (max - min + 1)
}
/// Generate random numbers without external dependencies
pub fn rand() -> u64 {
RandomState::new().build_hasher().finish()
}
See the Rust Playground
Upvotes: 0
Reputation: 430673
TL;DR:
use rand::{distributions::Uniform, Rng}; // 0.8.0
fn main() {
let range = Uniform::from(0..20);
let values: Vec<u64> = rand::thread_rng().sample_iter(&range).take(100).collect();
println!("{:?}", values);
}
It's important to use rand::distributions::uniform::Uniform
instead of simply performing the modulo of a uniform random number. See Why do people say there is modulo bias when using a random number generator? for more details.
Since we are generating multiple numbers from a range, it's more performant to create the Uniform
once and reuse it. Creating the Uniform
does some computation to avoid sampling bias.
We can use Rng::sample_iter
to create an iterator of random values and then take
some number of them, collecting into a Vec
. collect
will even make use of Iterator::size_hint
to allocate exactly the right number of elements.
If you only needed a single random number in the range, you could use the shortcut Rng::gen_range
:
use rand::Rng; // 0.8.0
fn main() {
let mut rng = rand::thread_rng();
let value: u64 = rng.gen_range(0..20);
}
If you needed a vector of random values without limiting to a range, you can use the Standard
distribution:
use rand::{distributions::Standard, Rng}; // 0.8.0
fn main() {
let values: Vec<u64> = rand::thread_rng().sample_iter(Standard).take(100).collect();
println!("{:?}", values);
}
Upvotes: 17
Reputation: 161457
There are a few main pieces that you need here. First, how to create a vector of 100 calculated items? The easiest way is to create a range of 100 and map over those items. For instance you could do:
let vals: Vec<u64> = (0..100).map(|v| v + 1000).collect();
// [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, ...
Splitting this up:
0..100
creates an iterator for 0 through 99.map
processes each item in the iterator when the iterator is processed..collect()
takes an iterator and converts it into any type that implements FromIterator
which in your case is Vec
.Expanding on this for your random values, you can adjust the .map
function to generate a random value from 0 to 20 using the rand
crate's gen_range
function to create a numeric value within a given range.
use rand::Rng; // 0.6.5
fn main() {
let mut rng = rand::thread_rng();
let vals: Vec<u64> = (0..100).map(|_| rng.gen_range(0, 20)).collect();
println!("{:?}", vals);
}
You should also consider using the rand::distributions::Uniform
type to create the range up front, which is is more efficient than calling gen_range
multiple times, then pull samples from it 100 times:
use rand::{distributions::Uniform, Rng}; // 0.6.5
fn main() {
let mut rng = rand::thread_rng();
let range = Uniform::new(0, 20);
let vals: Vec<u64> = (0..100).map(|_| rng.sample(&range)).collect();
println!("{:?}", vals);
}
Upvotes: 32