odem-rs 0.3.0

Object-based Discrete-Event Modelling in Rust using async/await
Documentation
//! This example demonstrates how to run many independent simulation replications
//! in parallel using Rayon.
//!
//! Each replication runs a simple experiment that tabulates random samples into
//! a `RandomVariable`. The results are then reduced across all replications
//! with `RandomVariable::join`. The `RngStream::par_iter()` iterator ensures
//! that each replication receives a statistically independent random number
//! stream, making the aggregate results reproducible regardless of thread
//! scheduling.

use odem_rs::{prelude::*, rayon::prelude::*};

#[derive(Config, Default)]
struct MyConfig {
	stats: RandomVariable<f64>,
}

async fn run_one(sim: &Sim<MyConfig>, rng_stream: RngStream) {
	let mut rng = rng_stream.rng();

	for _ in 0..10000 {
		sim.global().stats.tabulate(rng.random_range(0.0..1.0));
	}
}

fn run_many(count: usize) -> RandomVariable<f64> {
	RngStream::par_iter()
		.take(count) // <- perform `count` many experiments
		.map(|rng_stream| {
			simulation(async |sim| run_one(sim, rng_stream).await)
				.unwrap()
				.stats
		})
		.reduce(RandomVariable::new, RandomVariable::join)
}

fn main() {
	println!("{:#?}", run_many(1000));
}