The random! macro generates a pseudorandom value at compile time. It uses file, line, and column information along with optional seeds to ensure uniqueness.
Supported Types
- Integers:
u8, u16, u32, u64, usize, i8, i16, i32, i64, isize (generates values in their respective range). - Booleans:
bool. - Floats:
f32, f64 (generates values in the range [1.0, 2.0)).
Important Usage Notes
- Macro Nesting: When
random! is used inside the definition of another macro, it may not behave as expected (it might produce the same value for different calls) because the expansion happens at a different stage. To fix this, provide a unique literal seed. - Seeding: You can provide additional unique seeds to ensure different values when using the macro in repeated contexts or within other macros.
Example
// Basic usage
const RND: i32 = obfstr::random!(u8) as i32;
// Using unique seeds to ensure different values in macros
macro_rules! seeded {
() => {
assert_ne!(obfstr::random!(u64, "lhs"), obfstr::random!(u64, "rhs"));
};
}
seeded!();
const RND: i32 = obfstr::random!(u8) as i32;
assert!(RND >= 0 && RND <= 255);
// To ensure uniqueness in macros, provide literal seeds:
macro_rules! seeded {
() => {
assert_ne!(obfstr::random!(u64, "lhs"), obfstr::random!(u64, "rhs"));
};
}
seeded!();