dice API documentation
Project documentation Docs

File: dice/core.py

1# --------------------------------------------------------------------------------------------------2# SPDX-License-Identifier: Apache-2.03# SPDX-FileCopyrightText: 2026 cpdocs developers4# --------------------------------------------------------------------------------------------------5"""The die model."""6 7from __future__ import annotations8 9import random as _random10 11from ._rng import seeded12 13 14class Die:15    """A die with a fixed number of sides."""16 17    def __init__(self, sides: int, *, seed: int) -> None:18        """Create a die with sides faces whose rolls are reproducible from seed."""19        self.sides = sides20        self._rng: _random.Random = seeded(seed)21 22    def roll(self) -> int:23        """Return a face between 1 and sides."""24        return self._rng.randint(1, self.sides)25 26 27def roll(die: Die, times: int) -> list[int]:28    """Roll die times times."""29    return [die.roll() for _ in range(times)]