This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/nprandom/seedseq

NumPy's SeedSequence, the hash behind default_rng(seed)

np.random.default_rng(seed) does not use the seed as the state. It hands the seed to SeedSequence, which hashes its 32-bit words into a pool of four words and draws the generator's initial state from the pool. This is that code from NumPy 1.24.4 (numpy/random/bit_generator.pyx: hashmix, mix, mix_entropy, generate_state; tag v1.24.4, commit 9315a9072b2636f75c831b4eca9f42a5f67ca2fb), with its constants' names. Every product is a 32-bit product that wraps (npr_mul32).

  • npr_ss_words(n): the words of a non-negative int below 2^53, lowest first, as _int_to_uint32_array splits it ([0] for 0).
  • npr_ss_pool(entropy): mix_entropy of an array of words into the pool.
  • npr_ss_generate(pool, n_words): generate_state(n_words) as 32-bit words (for np.uint64 words, pair them up: word 2k is the low half of the k-th).

Used with import nprandom.seedseq.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 22–99