Crate random_number[−][src]
Random Number
Generate random numbers quickly.
The random!
Marco
#[macro_use] extern crate random_number; let n: u8 = random!(); println!("{}", n); // 0 ~ 255 let n: f64 = random!(); println!("{}", n); // 0.0 ~ 1.0 let n: u8 = random!(..=10); println!("{}", n); // 0 ~ 10 let n: u8 = random!(..=9); println!("{}", n); // 0 ~ 9 let n: u8 = random!(10..); println!("{}", n); // 10 ~ 255 let n: i8 = random!(-2..=12); println!("{}", n); // -2 ~ 12 let n: u8 = random!(12, 20); println!("{}", n); // 12 ~ 20 let n: u8 = random!(20, 12); println!("{}", n); // 12 ~ 20
The random number generator can be reused by adding it to the random!
macro as the last argument.
#[macro_use] extern crate random_number; let mut rng = random_number::rand::thread_rng(); let n: u8 = random!(rng); println!("{}", n); // 0 ~ 255 let n: u8 = random!(..=10, rng); println!("{}", n); // 0 ~ 10 let n: u8 = random!(20, 12, rng); println!("{}", n); // 12 ~ 20
The random_ranged
Function
If the range is not literal, for example, a variable, var_range
, storing an instance that implements the RangeBounds
trait, the var_range
variable cannot be used in the random!
macro.
#[macro_use] extern crate random_number; let var_range = 1..=10; let n: u8 = random!(var_range); // compile error
In this case, use the random_ranged
function instead.
extern crate random_number; let var_range = 1..=10; let n: u8 = random_number::random_ranged(var_range); println!("{}", n); // 1 ~ 10
The random_fill!
Marco
The random_fill!
marco can be used to fill a slice with random numbers. The usage is like the random!
macro. Just add a slice as the first argument when using the random_fill!
macro.
#[macro_use] extern crate random_number; let mut a = [0i8; 32]; random_fill!(a, -2..=12); println!("{:?}", a);
The random_fill_ranged
Function
extern crate random_number; let var_range = 1..=10; let mut a = [0u8; 32]; random_number::random_fill_ranged(&mut a, var_range); println!("{:?}", a);
Re-exports
pub extern crate rand; |
Macros
random | Generate a random number. |
random_fill | Generate random numbers. |
Traits
Bounded | The random range of different types. |
Functions
random | Generate a random value in the range of the output type with a new lazily-initialized thread-local random number generator. |
random_at_least | Generate a random value in the range [ |
random_at_least_with_rng | Generate a random value in the range [ |
random_at_most | Generate a random value in the range [ |
random_at_most_exclusively | Generate a random value in the range [ |
random_at_most_exclusively_with_rng | Generate a random value in the range [ |
random_at_most_with_rng | Generate a random value in the range [ |
random_exclusively | Generate a random value in the range [ |
random_exclusively_with_rng | Generate a random value in the range [ |
random_fill | Generate random values in the range of the output type with a new lazily-initialized thread-local random number generator. |
random_fill_at_least | Generate random values in the range [ |
random_fill_at_least_with_rng | Generate random values in the range [ |
random_fill_at_most | Generate random values in the range [ |
random_fill_at_most_exclusively | Generate random values in the range [ |
random_fill_at_most_exclusively_with_rng | Generate random values in the range [ |
random_fill_at_most_with_rng | Generate random values in the range [ |
random_fill_exclusively | Generate random values in the range [ |
random_fill_exclusively_with_rng | Generate random values in the range [ |
random_fill_inclusively | Generate random values in the range [ |
random_fill_inclusively_cmp | Generate random values in the range [ |
random_fill_inclusively_cmp_with_rng | Generate random values in the range [ |
random_fill_inclusively_with_rng | Generate random values in the range [ |
random_fill_ranged | Generate random values in a specific range with a new lazily-initialized thread-local random number generator. |
random_fill_ranged_with_rng | Generate random values in a specific range with an existing random number generator. |
random_fill_with_rng | Generate random values in the range of the output type with an existing random number generator. |
random_inclusively | Generate a random value in the range [ |
random_inclusively_cmp | Generate a random value in the range [ |
random_inclusively_cmp_with_rng | Generate a random value in the range [ |
random_inclusively_with_rng | Generate a random value in the range [ |
random_ranged | Generate a random value in a specific range with a new lazily-initialized thread-local random number generator. |
random_ranged_with_rng | Generate a random value in a specific range with an existing random number generator. |
random_with_rng | Generate a random value in the range of the output type with an existing random number generator. |