macro_rules! random {
($ty:ident) => { ... };
(u8, $seed:expr) => { ... };
(u16, $seed:expr) => { ... };
(u32, $seed:expr) => { ... };
(u64, $seed:expr) => { ... };
(usize, $seed:expr) => { ... };
(i8, $seed:expr) => { ... };
(i16, $seed:expr) => { ... };
(i32, $seed:expr) => { ... };
(i64, $seed:expr) => { ... };
(isize, $seed:expr) => { ... };
(bool, $seed:expr) => { ... };
(f32, $seed:expr) => { ... };
(f64, $seed:expr) => { ... };
($ty:ident, $seed:expr) => { ... };
}
Expand description
Compiletime random number generator.
Supported types are u8
, u16
, u32
, u64
, usize
, i8
, i16
, i32
, i64
, isize
, bool
, f32
and f64
.
The integer types generate a random value in their respective range.
The float types generate a random value in range of [1.0, 2.0)
.
While the result is generated at compiletime only the integer types are available in const contexts.
Note that the seed must be a uniformly distributed random u64
value.
If such a value is not available, see the splitmix
function to generate it from non uniform random value.
const RND: i32 = obfstr::random!(u8) as i32;
assert!(RND >= 0 && RND <= 255);
The random machinery is robust enough that it avoids exact randomness when mixed with other macros:
assert_ne!(obfstr::random!(u64), obfstr::random!(u64));