File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Retos/Reto #8 - EL GENERADOR PSEUDOALEATORIO [Media]/python Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Crea un generador de números pseudoaleatorios entre 0 y 100.
3+ - No puedes usar ninguna función "random" (o semejante) del
4+ lenguaje de programación seleccionado.
5+
6+ Es más complicado de lo que parece...
7+ """
8+
9+ from datetime import datetime
10+ from typing import Optional
11+
12+ def random_number_generator (
13+ seed : Optional [int ] = None ,
14+ * ,
15+ count : int = 100 ,
16+ a : int = 1664525 ,
17+ c : int = 1013904223 ,
18+ m : int = 101 ):
19+ """
20+ Generates a sequence of pseudo-random numbers between 0 and 100
21+ using a linear congruential generator algorithm (LCG).
22+
23+ Args:
24+ seed (Optional[int]): Initial seed for the generator. If not
25+ provided, the current microsecond is used.
26+ count (int): Number of random numbers to generate (default is 100).
27+ a (int): Multiplier constant for the algorithm (default is 1664525).
28+ c (int): Increment constant for the algorithm (default is 1013904223).
29+ m (int): Modulus to limit the range of numbers (default is 101).
30+
31+ Yields:
32+ int: Pseudo-random number in the range [0, 100].
33+ """
34+
35+ if seed is None :
36+ seed = datetime .now ().microsecond % m
37+
38+ x = seed
39+ for _ in range (count ):
40+ x = (a * x + c ) % m
41+ yield x
42+
43+
44+ if __name__ == "__main__" :
45+ for num in random_number_generator ():
46+ print (num )
You can’t perform that action at this time.
0 commit comments