Skip to content

Commit 74a1004

Browse files
docs(other): add doctest examples to linear_congruential_generator (addresses #9943)
1 parent 0876a87 commit 74a1004

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

other/linear_congruential_generator.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
"""
2+
Linear Congruential Generator (LCG)
3+
https://en.wikipedia.org/wiki/Linear_congruential_generator
4+
5+
This module implements a simple linear congruential generator. It is intended
6+
for educational purposes, not for cryptographic use.
7+
8+
>>> lcg = LinearCongruentialGenerator(2, 3, 9, seed=1)
9+
>>> [lcg.next_number() for _ in range(5)]
10+
[5, 4, 2, 7, 8]
11+
12+
The generated values are always in the half-open interval [0, modulo):
13+
14+
>>> lcg = LinearCongruentialGenerator(2, 3, 9, seed=1)
15+
>>> all(0 <= lcg.next_number() < 9 for _ in range(10))
16+
True
17+
"""
18+
119
__author__ = "Tobias Carryer"
220

321
from time import time
@@ -28,9 +46,15 @@ def __init__(self, multiplier, increment, modulo, seed=int(time())): # noqa: B0
2846

2947
def next_number(self):
3048
"""
49+
Generate the next number in the sequence.
50+
3151
The smallest number that can be generated is zero.
32-
The largest number that can be generated is modulo-1. modulo is set in the
33-
constructor.
52+
The largest number that can be generated is ``modulo - 1``. ``modulo`` is
53+
set in the constructor.
54+
55+
>>> lcg = LinearCongruentialGenerator(5, 1, 16, seed=0)
56+
>>> [lcg.next_number() for _ in range(4)]
57+
[1, 6, 15, 12]
3458
"""
3559
self.seed = (self.multiplier * self.seed + self.increment) % self.modulo
3660
return self.seed

0 commit comments

Comments
 (0)