Skip to main content

Crate montycarlo

Crate montycarlo 

Source
Expand description

Generic Monte Carlo simulation engine.

This module provides a domain-agnostic Monte Carlo framework that can be used for any statistical analysis. The design is generic over the sample type and result type, making it reusable beyond RF regulatory analysis.

§Architecture

  • Simulation — defines how to generate a random sample and evaluate it
  • MonteCarloEngine — runs N trials, collects results, computes statistics
  • MonteCarloResult — holds raw results and provides statistical queries

§Example

use montycarlo::{MonteCarloEngine, Simulation};
use rand::Rng;

// Simulate rolling two dice and summing them
struct DiceRoll;

impl Simulation for DiceRoll {
    type Sample = (u32, u32);
    type Output = f64;

    fn sample(&self, rng: &mut impl Rng) -> Self::Sample {
        (rng.r#gen_range(1..=6), rng.r#gen_range(1..=6))
    }

    fn evaluate(&self, sample: &Self::Sample) -> Self::Output {
        (sample.0 + sample.1) as f64
    }
}

let engine = MonteCarloEngine::new(DiceRoll, 10_000);
let result = engine.run();
assert!((result.mean() - 7.0).abs() < 0.5);
assert_eq!(result.len(), 10_000);

Structs§

MonteCarloEngine
Engine that runs a Simulation for N trials and collects results.
MonteCarloResult
Results from a Monte Carlo simulation run.

Traits§

Simulation
Defines a Monte Carlo simulation.