Function random_choice

Source
pub fn random_choice<T>(slice: &[T]) -> Option<&T>
Expand description

Returns a randomly chosen item from a list of options.

You should use this value immediately rather than using it later. If you delay, then it is possible for the simulation to branch in between receiving the random data and using it. These branches will have the same random value, which defeats the purpose of branching.

Similarly, do not use the value to seed a pseudo-random number generator. The PRNG will produce a deterministic sequence of pseudo-random values based on the seed, so if the simulation branches, the PRNG will use the same sequence of values in all branches.

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}'");
};