“Because RNGesus deserves O(1) performance.”
droptables is a Rust library for building and sampling from weighted drop tables at lightning speed.
It’s perfect for games, loot systems, procedural generation, or anywhere you need weighted random picks—without sacrificing performance or your sanity.
- ⚡ O(1) Sampling – Uses Walker’s Alias Method for constant-time draws.
- 📦 Enum Power-Up – Derive probabilities directly from enum variants with
#[weight(...)]. - 🔮 Flexible Sources – Build from enums or from arbitrary
(item, weight)pairs. - 🛡️ Error-checked – Prevents negative weights, zero-sum disasters, and other statistical crimes.
- 🥷 No Cloning Required – Sample by reference or by value.
Add to your Cargo.toml:
[dependencies]
droptables = "0.1"use droptables::{DropTable, WeightedEnum};
use std::collections::HashMap;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, WeightedEnum)]
enum Rarity {
#[odds = "1/1000"]
Mythic,
#[odds = "1/100"]
Legendary,
#[odds = "20/100"]
Uncommon,
#[rest]
Common,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let table = Rarity::droptable()?;
let mut hist: HashMap<Rarity, u64> = HashMap::new();
let mut rng = rand::rng();
for _ in 0..2000 {
*hist.entry(*table.sample(&mut rng)).or_default() += 1;
}
for (rarity, count) in hist {
println!("{count:>5} {:?}", rarity);
}
Ok(())
}Under the hood:
WeightedEnummacro scans your enum variants for#[odds = "1/100"]attributes.- Probabilities are compiled into a static
ENTRIESarray. DropTablebuilds an alias table viaWeightedSamplerfor O(1) sampling.- You call
.sample()and get your item fast.
cargo run --example raritySample output:
997 Common
402 Uncommon
58 Legendary
1 Mythic
(Your mileage may vary, depending on the whims of RNGesus.)
- 🎮 Game loot systems
- 🗺 Procedural map generation
- 🎲 Random event systems
- 🦄 Gacha mechanics (don’t be evil)
MIT — because sharing is caring.