From 74a1004bc23ffd8ee19d9c56b2f271f87a4bfeb6 Mon Sep 17 00:00:00 2001 From: jihanurrahman33 Date: Thu, 4 Sep 2025 01:58:54 +0600 Subject: [PATCH] docs(other): add doctest examples to linear_congruential_generator (addresses #9943) --- other/linear_congruential_generator.py | 28 ++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/other/linear_congruential_generator.py b/other/linear_congruential_generator.py index c7de15b94bbd..52a6941c0a77 100644 --- a/other/linear_congruential_generator.py +++ b/other/linear_congruential_generator.py @@ -1,3 +1,21 @@ +""" +Linear Congruential Generator (LCG) +https://en.wikipedia.org/wiki/Linear_congruential_generator + +This module implements a simple linear congruential generator. It is intended +for educational purposes, not for cryptographic use. + +>>> lcg = LinearCongruentialGenerator(2, 3, 9, seed=1) +>>> [lcg.next_number() for _ in range(5)] +[5, 4, 2, 7, 8] + +The generated values are always in the half-open interval [0, modulo): + +>>> lcg = LinearCongruentialGenerator(2, 3, 9, seed=1) +>>> all(0 <= lcg.next_number() < 9 for _ in range(10)) +True +""" + __author__ = "Tobias Carryer" from time import time @@ -28,9 +46,15 @@ def __init__(self, multiplier, increment, modulo, seed=int(time())): # noqa: B0 def next_number(self): """ + Generate the next number in the sequence. + The smallest number that can be generated is zero. - The largest number that can be generated is modulo-1. modulo is set in the - constructor. + The largest number that can be generated is ``modulo - 1``. ``modulo`` is + set in the constructor. + + >>> lcg = LinearCongruentialGenerator(5, 1, 16, seed=0) + >>> [lcg.next_number() for _ in range(4)] + [1, 6, 15, 12] """ self.seed = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed