feat: display all randomness (#7)

Co-authored-by: pwgen2155 <git@example.com>
Reviewed-on: pwgen2155/dawdle#7
This commit is contained in:
pwgen2155 2024-03-04 09:06:51 +00:00
parent 852caf73e5
commit 6b2c711ee6
Signed by: GammaSpectra.live Git
GPG key ID: 8B02E6093E9CB7B3

View file

@ -1,9 +1,8 @@
import random
from typing import cast, Any, Dict, List, MutableSequence, Sequence, TypeVar
from dawdle.log import log
T = TypeVar("T")
overrides: Dict[str,Any] = {}
@ -11,27 +10,37 @@ def randomly(key: str, odds: int) -> bool:
"""Overrideable random func which returns true at 1:ODDS odds."""
if key in overrides:
return cast(bool, overrides[key])
return random.randint(0, odds-1) < 1
ans = random.randint(0, odds-1) < 1
log.debug(f"Rand: {key}: {odds} A: {ans}")
return ans
def randint(key: str, bottom: int, top: int) -> int:
"""Overrideable random func which returns an integer bottom <= i <= top."""
if key in overrides:
return cast(int, overrides[key])
return random.randint(int(bottom), int(top))
ans = random.randint(int(bottom), int(top))
# Very Very Verbose...
if key not in ['move_player_x', 'move_player_y']:
log.debug(f"Rand: {key}: {int(bottom)} to {int(top)} A: {ans}")
return ans
def gauss(key: str, mu: float, sigma: float) -> int:
"""Overrideable func which returns an random int with gaussian distribution."""
if key in overrides:
return cast(int, overrides[key])
return int(random.gauss(mu, sigma))
ans = int(random.gauss(mu, sigma))
log.debug(f"Rand: {key}: A: {ans}")
return
def sample(key: str, seq: Sequence[T], count: int) -> List[T]:
"""Overrideable random func which returns random COUNT elements of SEQ."""
if key in overrides:
return cast(List[T], overrides[key])
return random.sample(seq, count)
ans = random.sample(seq, count)
log.debug(f"Rand: {key}: {seq} A:{ans}")
return
def choice(key: str, seq: Sequence[T]) -> T:
@ -40,7 +49,9 @@ def choice(key: str, seq: Sequence[T]) -> T:
return cast(T, overrides[key])
# Don't use random.choice here - it uses random access, which
# is unsupported by the dict_keys view.
return random.sample(seq, 1)[0]
ans = random.sample(seq, 1)[0]
log.debug(f"Rand: {key}: {seq} A: {ans}")
return
def shuffle(key: str, seq: MutableSequence[Any]) -> None: