A "nice" random library for games - random selection with memory to prevent frustrating repeats.
pip install nicerandOr from source:
python3.12 -m venv .venv
source .venv/bin/activate
pip install -e .from nicerand import NiceRandom
# Default cooldown is 1 (no immediate repeats)
picker = NiceRandom(["a", "b", "c", "d", "e"])
item = picker.pick() # Won't get the same item twice in a row# Cooldown of 3: an item won't repeat until 3 others have been picked
picker = NiceRandom(["sword", "shield", "potion", "gold", "gem"], cooldown=3)# Shuffle bag: all items appear before any can repeat
pieces = ["I", "O", "T", "S", "Z", "J", "L"]
bag = NiceRandom(pieces, cooldown=len(pieces))
# Every 7 picks will contain all 7 pieces exactly once
round1 = bag.pick_n(7) # All pieces, random order
round2 = bag.pick_n(7) # All pieces again, different order# Common items appear more often, but still respect cooldown
loot_table = NiceRandom({
"gold": 10, # 10x weight - very common
"potion": 5, # 5x weight - common
"sword": 2, # 2x weight - uncommon
"legendary": 1, # 1x weight - rare
}, cooldown=2)
drop = loot_table.pick()picker = NiceRandom(["a", "b", "c", "d"])
items = picker.pick_n(10) # Pick 10 itemspicker = NiceRandom(["a", "b", "c", "d"], seed=42)pip install -e .
python examples/basic_usage.pypip install -e ".[dev]"
pytestWith coverage:
pytest --cov=nicerand --cov-report=term- Python 3.12+