Pseudo-random numbers/PCG32: Difference between revisions

→‎Python: As generator: Generates floats as well as ints.
(→‎{{header|Python}}: Add generator version.)
(→‎Python: As generator: Generates floats as well as ints.)
Line 968:
 
===Python: As generator===
(Must divide int by 1 << 32 to get float values).
 
<lang python>mask64def pcg32(seed_state=None, (1seed_sequence=None, << 64as_int=True) - 1:
mask32 = (1 << 32) - 1
CONST = 6364136223846793005
 
 
def pcg32(seed_state=None, seed_sequence=None):
def next_int():
"return random 32 bit unsigned int"
Line 996 ⟶ 990:
 
while True:
yield next_int() if as_int else next_int() / (1 << 32)
 
 
Line 1,005 ⟶ 999:
print(i)
hist = {i:0 for i in range(5)}
for i in islice(pcg32(987654321, 1, as_int=False), 100_000):
hist[int(i / (1 << 32) * 5)] += 1
print(hist)</lang>
 
Anonymous user