1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
use rand::{Error, RngCore};
use crate::internal;
/// Returns a u64 value chosen by Antithesis. You should not
/// store this value or use it to seed a PRNG, but should use it
/// immediately.
///
/// # Example
///
/// ```
/// use antithesis_sdk::random;
///
/// let value = random::get_random();
/// println!("Random value(u64): {value}");
/// ```
pub fn get_random() -> u64 {
internal::dispatch_random()
}
/// Returns a randomly chosen item from a list of options. You
/// should not store this value, but should use it immediately.
///
/// This function is not purely for convenience. Signaling to
/// the Antithesis platform that you intend to use a random value
/// in a structured way enables it to provide more interesting
/// choices over time.
///
/// # Example
///
/// ```
/// use antithesis_sdk::random;
///
/// let choices: Vec<&str> = vec!["abc", "def", "xyz", "qrs"];
/// if let Some(s) = random::random_choice(choices.as_slice()) {
/// println!("Choice: '{s}'");
/// };
/// ```
pub fn random_choice<T>(slice: &[T]) -> Option<&T> {
match slice {
[] => None,
[x] => Some(x),
_ => {
let idx: usize = (get_random() as usize) % slice.len();
Some(&slice[idx])
}
}
}
/// A random number generator that uses Antithesis's random number generation.
///
/// This implements the `RngCore` trait from the `rand` crate, allowing it to be used
/// with any code that expects a random number generator from that ecosystem.
///
/// # Example
///
/// ```
/// use antithesis_sdk::random::AntithesisRng;
/// use rand::{Rng, RngCore};
///
/// let mut rng = AntithesisRng;
/// let random_u32: u32 = rng.gen();
/// let random_u64: u64 = rng.gen();
/// let random_char: char = rng.gen();
///
/// let mut bytes = [0u8; 16];
/// rng.fill_bytes(&mut bytes);
/// ```
pub struct AntithesisRng;
impl RngCore for AntithesisRng {
fn next_u32(&mut self) -> u32 {
get_random() as u32
}
fn next_u64(&mut self) -> u64 {
get_random()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
// Split the destination buffer into chunks of 8 bytes each
// (since we'll fill each chunk with a u64/8 bytes of random data)
let mut chunks = dest.chunks_exact_mut(8);
// Fill each complete 8-byte chunk with random bytes
for chunk in chunks.by_ref() {
// Generate 8 random bytes from a u64 in native endian order
let random_bytes = self.next_u64().to_ne_bytes();
// Copy those random bytes into this chunk
chunk.copy_from_slice(&random_bytes);
}
// Get any remaining bytes that didn't fit in a complete 8-byte chunk
let remainder = chunks.into_remainder();
if !remainder.is_empty() {
// Generate 8 more random bytes
let random_bytes = self.next_u64().to_ne_bytes();
// Copy just enough random bytes to fill the remainder
remainder.copy_from_slice(&random_bytes[..remainder.len()]);
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::{HashMap, HashSet};
use rand::Rng;
use rand::seq::SliceRandom;
#[test]
fn random_choice_no_choices() {
let array = [""; 0];
assert_eq!(0, array.len());
assert_eq!(None, random_choice(&array))
}
#[test]
fn random_choice_one_choice() {
let array = ["ABc"; 1];
assert_eq!(1, array.len());
assert_eq!(Some(&"ABc"), random_choice(&array))
}
#[test]
fn random_choice_few_choices() {
// For each map key, the value is the count of the number of
// random_choice responses received matching that key
let mut counted_items: HashMap<&str, i64> = HashMap::new();
counted_items.insert("a", 0);
counted_items.insert("b", 0);
counted_items.insert("c", 0);
let all_keys: Vec<&str> = counted_items.keys().cloned().collect();
assert_eq!(counted_items.len(), all_keys.len());
for _i in 0..15 {
let rc = random_choice(all_keys.as_slice());
if let Some(choice) = rc {
if let Some(x) = counted_items.get_mut(choice) {
*x += 1;
}
}
}
for (key, val) in counted_items.iter() {
assert_ne!(*val, 0, "Did not produce the choice: {}", key);
}
}
#[test]
fn get_random_100k() {
let mut random_numbers: HashSet<u64> = HashSet::new();
for _i in 0..100000 {
let rn = get_random();
assert!(!random_numbers.contains(&rn));
random_numbers.insert(rn);
}
}
#[test]
fn rng_no_choices() {
let mut rng = AntithesisRng;
let array = [""; 0];
assert_eq!(0, array.len());
assert_eq!(None, array.choose(&mut rng));
}
#[test]
fn rng_one_choice() {
let mut rng = AntithesisRng;
let array = ["ABc"; 1];
assert_eq!(1, array.len());
assert_eq!(Some(&"ABc"), array.choose(&mut rng));
}
#[test]
fn rng_few_choices() {
let mut rng = AntithesisRng;
// For each map key, the value is the count of the number of
// random_choice responses received matching that key
let mut counted_items: HashMap<&str, i64> = HashMap::new();
counted_items.insert("a", 0);
counted_items.insert("b", 0);
counted_items.insert("c", 0);
let all_keys: Vec<&str> = counted_items.keys().cloned().collect();
assert_eq!(counted_items.len(), all_keys.len());
for _i in 0..15 {
let rc = all_keys.choose(&mut rng);
if let Some(choice) = rc {
if let Some(x) = counted_items.get_mut(choice) {
*x += 1;
}
}
}
for (key, val) in counted_items.iter() {
assert_ne!(*val, 0, "Did not produce the choice: {}", key);
}
}
#[test]
fn rng_100k() {
let mut rng = AntithesisRng;
let mut random_numbers: HashSet<u64> = HashSet::new();
for _i in 0..100000 {
let rn: u64 = rng.gen();
assert!(!random_numbers.contains(&rn));
random_numbers.insert(rn);
}
}
}