simple_mcts/
utils.rs

1use rand::{self, rngs::StdRng, Rng};
2
3/// Samples an action index from a given policy distribution using a random number generator.
4///
5/// This function performs a weighted random selection, where actions with higher
6/// policy probabilities are more likely to be chosen.
7///
8/// # Parameters
9/// - `policy`: A slice representing the probability distribution over actions.
10///             The sum of probabilities should ideally be 1.0.
11/// - `rng`: A mutable reference to a `StdRng` (standard random number generator)
12///          instance. This allows for reproducible sampling if the RNG is seeded.
13///
14/// # Returns
15/// The index of the sampled action.
16///
17/// # Panics
18/// Panics if the `policy` slice is empty and if no action is selected (should not happen
19/// if policy sums to 1.0).
20pub fn sample(policy: &[f64], rng: &mut StdRng) -> usize{
21    let mut random: f64 = rng.random();
22
23    policy.iter().position(|&x|{
24        random -= x;
25        random <= 0.
26    }).unwrap_or(policy.len()-1)
27}