Repository navigation
feat(random): numpy.random with NumPy's numbers for the same seed - #45
Merged
Merged
Conversation
Package random is numpy.random on arrays: SeedSequence; the bit generators PCG64 (default), PCG64DXSM, MT19937, Philox and SFC64 with jumped/advance; Generator (DefaultRNG) with random, integers (Lemire, buffered 8/16-bit draws), ziggurat normal/exponential (float64 and float32), gamma, beta, binomial, Poisson, choice (Floyd and weighted), shuffle, permutation, permuted, bytes, dirichlet, multinomial and 25 more samplers; and the legacy RandomState (MT19937, polar Gaussian, masked randint, NumPy 1.16 binomial) behind random.Seed / random.Rand / random.Randn ... Checked against NumPy 2.5.3 (x86-64): SeedSequence pools and spawns, raw streams and jumps of all five generators, 2,492 calls (81,806 values) over 6 seeds, and 50 long runs by digest (4.49M values, ziggurat tails included). Integers and arithmetic-only samplers match to the bit; log, log1p, exp, expm1 and pow are rounded correctly here (Ziv fast path plus double-double), which leaves 0.03-0.5% of values one ULP from NumPy where its C library misrounds. Passes unchanged on arm64, amd64, 386, arm, riscv64, ppc64le and s390x. Coverage 100%. NumPy's own aarch64 build differs from its x86-64 build (FMA contraction): 20.8% of uniform values, 14% of RandomState.randn. docs/random.md has the measurements, negative controls and performance (Go 12-36% slower than NumPy for the Generator, 2.1x for the legacy randn). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ebooks bullets) Resolved through the API: github.com refused git from this host (403). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A new subpackage,
random:numpy.randomon go-ndarray arrays, giving NumPy's numbers for the same seed, so a notebook migrated from NumPy keeps its results.SeedSequence(pool,generate_stateuint32/uint64,spawn, spawn keys, pool size).PCG64(default),PCG64DXSM,MT19937,Philox,SFC64, withJumped/Advance/RandomRaw/Spawn/state.Generator:Random(float64/float32),Integers(all 9 integer/bool dtypes,endpoint; Lemire with the buffered 8/16-bit draws),Uniform, zigguratStandardNormal/NormalandStandardExponential/Exponential(float64 and float32),StandardGamma/Gamma,Beta,ChiSquare,Binomial,Poisson,Geometric,Lognormal,Laplace,Logistic,Choice(replace,p,axis,shuffle),Shuffle,Permutation,Permuted,Bytes,Dirichlet,Multinomial, plus F, noncentral χ²/F, t, Cauchy, Gumbel, Weibull, Pareto, power, Rayleigh, Wald, triangular, von Mises, Zipf, logseries, negative binomial.RandomState(MT19937 legacy seeding, polar Gaussian with its cached value, maskedrandint, NumPy 1.16 binomial) and the module functionsSeed,Rand,Randn,Randint,RandomSample,Normal,Uniform,Choice,Shuffle,Permutation,Binomial,Poisson.Base: #42 is merged; this targets
main(rebased on #43).Verification
random/testdata/cases.py, embedded: SeedSequence (10 entropies), raw streams/jumps/advance/spawn of all 5 generators × 6 seeds (0, 1, 42, 2⁶⁴−1,[1,2,3], 2¹²⁸+12345), legacy MT19937 seeding, 2,492 calls / 81,806 values, and 50 long runs by SHA-256 (4.49 M values; ziggurat tails and wedges included).rand/randint/binomial/poisson…).log/log1p/exp/expm1/poware implemented here correctly rounded (Ziv fast path ~2⁻⁶⁸ + double-double fallback; 0 misrounded on 63,000 mpmath-checked arguments), deterministic on every arch. Residual: 0.03–0.5% of values one ULP from NumPy, each a NumPy libm misrounding (checked with mpmath); with Go'smathit was 3–10%. Bounded per sampler in the test (libmULP), never globally.Bytes= NumPy's little-endian bytes) under qemu.MIX_MULT_L/MULT_A, a ziggurat entry, masked↔Lemire, MT seeding position, legacy BTPE signs) all fail the suite; one does not (legacy BTPE constant 13680→13860: a ~2·10⁻⁹ term that flips nothing in 10⁶ draws) — documented.go test -race -coverpkg=all→ 100.0%;go vet(native, linux/386, js/wasm, windows),GOOS=linux GOARCH=s390x go test -c,gofmt -l .empty.Finding: NumPy's aarch64 build disagrees with its own x86-64 build (FMA contraction, different libm): 20.8% of
uniformvalues, 14% of legacyrandn. This package follows the x86-64 (unfused) arithmetic on every architecture.Performance (Apple M4 Max, loaded host, 7 interleaved rounds, medians, ms per 10⁶)
randomrandom(float32)integers(0, 1000)standard_normalrandnSlower everywhere; details and causes in
docs/random.md.Not done
Broadcast (array) parameters and bounds,
out=,hypergeometric,multivariate_normal,multivariate_hypergeometric, the RandomState samplers beyond those listed (their 1.16 versions were not ported), pickling/state dicts as Python objects.🤖 Generated with Claude Code