Skip to content

Commit

Permalink
Add randint
Browse files Browse the repository at this point in the history
  • Loading branch information
fxrlxrn committed Jun 3, 2023
1 parent 019fd44 commit 9691a3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 12 additions & 1 deletion pystd/random.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
_seed = 0


def seed(a: int, version: int = 2):
...
global _seed
_seed = a


def getstate() -> object:
Expand All @@ -20,3 +24,10 @@ def getrandbits(k) -> int:

def random() -> float:
...


def randint(a: int, b: int) -> int:
assert a <= b
global _seed
_seed = (69069 * _seed + 1) % 2**32
return _seed % (b + 1 - a) + a
14 changes: 12 additions & 2 deletions tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
def test_same_seed_same_random():
random.seed(4)
a = random.random()
assert type(a) is int
assert type(a) is float
random.seed(4)
b = random.random()
assert type(b) is int
assert type(b) is float
assert a == b


Expand All @@ -25,3 +25,13 @@ def test_randbits_len():
assert type(bits) is int
assert bits >= 0
assert bits.bit_count() <= i


def test_randint():
random.seed(10)

for i in range(10000):
a = random.randint(0, i)
b = random.randint(a+1, (i+1)**2)
n = random.randint(min(a, b), max(a, b))
assert a <= n <= b

0 comments on commit 9691a3c

Please sign in to comment.