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/generator

NumPy's Generator methods, over PCG64

rng = np.random.default_rng(seed) and its methods random, uniform, integers, standard_normal, normal, choice, shuffle and permutation. Each def is the NumPy 1.24.4 code path it names (numpy/random/_generator.pyx, _bounded_integers.pyx.in, src/distributions/distributions.c; tag v1.24.4, commit 9315a9072b2636f75c831b4eca9f42a5f67ca2fb), drawing from the PCG64 state of lib/nprandom/pcg64 in the same order.

  • npr_gn_bounded(st, rng): an int in [0, rng] by Lemire's method (random_bounded_uint64 without the mask): no draw when rng is 0, one buffered 32-bit draw per try while rng < 2^32 - 1, the 32-bit draw itself at 2^32 - 1, a 64-bit draw and its 128-bit product with rng + 1 above (the product's halves are wrap_mul and umulhi; rng below 2^53 here). Generator.integers(low, high) is low + npr_gn_bounded(high - 1 - low).
  • npr_gn_interval(st, mx): random_interval, the masked draw shuffle uses.
  • npr_gn_shuffle, npr_gn_shuffle_rows: Fisher-Yates from the top index down.
  • npr_gn_shuffle_int(st, a, n, first): _shuffle_int, the Lemire-drawn Fisher-Yates that choice(replace=False) uses.
  • npr_gn_choice_noreplace(st, pop, size, do_shuffle): choice without p and without replacement, by Floyd's algorithm with a shuffle after it (when do_shuffle is 1), or by a partial shuffle of arange(pop) when pop > 10000 and size > pop // 50.
  • npr_gn_normal(st): random_standard_normal, the 256-strip ziggurat (lib/nprandom/ziggurat_tables). It returns NaN in two cases, where NumPy's value depends on the platform's libm, which Viv's libm does not reproduce bit for bit: a draw in the base strip's tail (NumPy calls log1p; about 1 draw in 4000), and a wedge test whose draw lies within 8 ulp of exp(-x^2/2) (never seen; the comparison could go either way on another libm). Everywhere else the value is NumPy's.

Used with import nprandom.generator.

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 37–241