stackforge_core/utils/random.rs
1//! Random data generation utilities (feature-gated).
2
3#[cfg(feature = "rand")]
4use rand::Rng;
5
6/// Generate random bytes.
7#[cfg(feature = "rand")]
8#[must_use]
9pub fn random_bytes(len: usize) -> Vec<u8> {
10 let mut rng = rand::rng();
11 (0..len).map(|_| rng.random()).collect()
12}
13
14/// Generate a random MAC address.
15#[cfg(feature = "rand")]
16#[must_use]
17pub fn random_mac() -> crate::MacAddress {
18 let mut rng = rand::rng();
19 let mut bytes = [0u8; 6];
20 rng.fill(&mut bytes);
21 // Set locally administered bit, clear multicast bit
22 bytes[0] = (bytes[0] | 0x02) & 0xFE;
23 crate::MacAddress::new(bytes)
24}