class

Krikri::PyRandom

Inherits Reference < Object

Constants

LOWER_MASK = 2147483647_u32
M = 397
MATRIX_A = 2567483615_u32
N = 624
UPPER_MASK = 2147483648_u32

Constructors

new(seed : String | Int)
Source

Class methods

bytes_key_words(payload : Bytes) : Array(UInt32)

Splits a big-endian byte payload into 32-bit words, lowest word first (the layout _PyLong_AsByteArray produces for the integer int.from_bytes(payload, 'big')), trimming leading zero words the way _PyLong_NumBits-derived keyused does. Byte indices past the start of the payload contribute zero.

Source
int_key_words(seed : Int64) : Array(UInt32)

An int seed is used as-is; its 32-bit words go into init_by_array lowest first (little-endian), leading zero words dropped.

Source
seed_key_words(seed : String) : Array(UInt32)

Lib/random.py seed(): a str seed becomes int.from_bytes(seed.encode() + sha512(seed.encode()).digest(), 'big'). The digest is appended to the utf-8 bytes, then the whole payload is one big-endian integer.

Source

Instance methods

choice(list : Array(V)) : V forall V

random.Random.choice(seq) - Python 3.11+ form (index via _randbelow).

Source
getrandbits(k : Int) : Int64

_randommodule.c's getrandbits: words = (k - 1) / 32 + 1 words assembled little-endian, with the final word's low bits dropped (r >>= (32 - k)) when the bit count isn't a multiple of 32. Supports up to 64 bits, far beyond any randrange/choice use here.

Source
randbelow(n : Int) : Int64

Lib/random.py _randbelow_with_getrandbits.

Source
randrange(stop : Int) : Int64

random.Random.randrange(stop) for the one-argument form: the width itself is the exclusive upper bound.

Source