7 releases

0.1.6 Apr 22, 2021
0.1.5 Apr 22, 2021
0.1.4 Mar 11, 2021
0.1.3 Jul 29, 2020
0.1.2 Jun 27, 2020

#178 in Rust patterns

Download history 979/week @ 2021-08-11 995/week @ 2021-08-18 1747/week @ 2021-08-25 2225/week @ 2021-09-01 5081/week @ 2021-09-08 6678/week @ 2021-09-15 10901/week @ 2021-09-22 4283/week @ 2021-09-29 1427/week @ 2021-10-06 1330/week @ 2021-10-13 1411/week @ 2021-10-20 1311/week @ 2021-10-27 1241/week @ 2021-11-03 1609/week @ 2021-11-10 1325/week @ 2021-11-17 1518/week @ 2021-11-24

5,904 downloads per month
Used in 5 crates (3 directly)

MIT license

43KB
731 lines

Random Number

CI

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);

Crates.io

https://crates.io/crates/random-number

Documentation

https://docs.rs/random-number

License

MIT

Dependencies

~0.6–1MB
~24K SLoC