//! dice notation parser, roller, and probability engine for TTRPGs //! //! # quick start //! //! ```rust //! use ttrpg_dice_engine::{roll, distribution, engine::LiveRng}; //! //! // roll 4d6, keep highest 3 (DnD ability score generation for ex) //! let mut rng = LiveRng::new(); //! let result = roll("4d6kh3", &mut rng).unwrap(); //! println!("Rolled {} (mean {:.1})", result.total, result.distribution_position.mean); //! //! // compute theoretical distribution without rolling //! let dist = distribution("2d6").unwrap(); //! assert!((dist.mean - 7.0).abs() < 0.001); //! ``` //! //! # Notation //! //! | Syntax | Meaning | //! |--------|---------| //! | `NdX` | Roll N dice with X sides | //! | `dX` | Roll one X-sided die | //! | `NdF` / `NdF` | FATE/Fudge dice (outcomes −1, 0, +1) | //! | `d%` | Percentile die (d100) | //! | `kh N` / `k N` | Keep highest N | //! | `kl N` | Keep lowest N | //! | `dh N` | Drop highest N | //! | `dl N` | Drop lowest N | //! | `!` | Explode on max | //! | `!>=N` | Explode on result ≥ N | //! | `!!` | Compounding explosion | //! | `r N` | Reroll while equal to N | //! | `ro N` | Reroll once if equal to N | //! | `mi N` / `ma N` | Minimum / maximum result per die | //! | `>N` `>=N` ` Result { let expr = parse(notation)?; let dist = compute_distribution(&expr, notation)?; let breakdown = eval_expr(&expr, rng); let total = breakdown.value(); let position = dist.position_of(total); Ok(RollResult { notation: notation.to_string(), total, breakdown, distribution_position: position, }) } /// compute the theoretical probability distribution for notation w/out rolling /// /// returns the exact PMF, mean, variance, standard deviation, and common percentiles /// /// # Errors /// /// returns [`EngineError::Parse`] for invalid notation, or [`EngineError::TooComplex`] for /// expressions the exact engine cannot handle. /// /// # Example /// /// ```rust /// use ttrpg_dice_engine::distribution; /// let dist = distribution("2d6").unwrap(); /// assert!((dist.mean - 7.0).abs() < 0.001); /// assert_eq!(dist.min, 2); /// assert_eq!(dist.max, 12); /// ``` pub fn distribution(notation: &str) -> Result { let expr = parse(notation)?; compute_distribution(&expr, notation) }