Changelog
Source:NEWS.md
zurand 0.1.0
First release. Stateless random numbers: every value is a pure function of an immutable key and the draw’s position, so output is bit-identical across platforms, thread counts, SIMD paths and call order.
The stream (frozen as stream-1)
- Three engines.
xoshiro256pp, the default, is xoshiro256++ reseeded every 512 values from Philox4x64-10;philox4x64andthreefry4x64are the Random123 counter-based generators, available directly. - Every key records its engine and a stream version (
attr(key, "stream"), currently 1), so a stream can be corrected after release without changing what existing keys produce. - Normal draws use a ziggurat on NumPy’s tables with zurand’s own port of fdlibm’s
exp()andlog1p(), and no fused multiply-add on any compiler, so they are identical on Linux (glibc and musl), macOS and Windows, on x86_64 and aarch64. 32-bit x87 is outside that guarantee. -
xoshiro256pppassed a PractRand audit clean to 1 TB, including interleaved streams of sibling and folded keys.
Functions
-
rng_key(),rng_key_from_r()andrng_fold()create and derive keys.rng_fold()folds the storage type and values of plain vectors; classed data such as factors and dates is an error, since a factor would fold as codes that change with its levels (#60). - Every error zurand raises has the class
zurand_error, so it can be caught by class rather than by message (#62). -
rng_fold()folds a string’s text in UTF-8 whatever the locale: in the C locale an unmarked non-ASCII string used to fold as R’s<c3><a9>escapes, a different key from the same script in a UTF-8 session (#123). - Classed numbers – factors,
integer64, dates, difftimes – are refused on every numeric argument instead of being read as their storage value, sorng_key(factor("5"))is no longer seed 1 (#126). - Key vectors: arithmetic,
Math,Summary,t()andnames<-are errors, since they made other, valid keys (#125);==and!=compare whole keys;head(),tail(),match(),%in%andas.character()work key by key;keys[NULL]is an empty key vector;keys[[i, j]]is an error (#128, #129, #136). An invalidstreamattribute is refused by[andc()as by the samplers (#130). - A zero-length key vector with
nabove.Machine$integer.maxis the multi-key dimension error rather than a truncated matrix (#124). -
rng_simd()accepts onlyNULL,TRUE,FALSEor a value it returned, sorng_simd(old)restores (#131). - A key vector behaves as a vector of opaque keys:
[,[[,c(),rev(),rep(),unique(),duplicated()andas.list()work key by key, solapply(keys, f)and parallel maps hand each task one key. Keys are immutable: assigning into a key vector is an error, as is an index that is infinite or beyond the vector, which R would otherwise turn into a key of missing words shared by every vector (#57). A key vector takes one index:keys[1, 2]andkeys[, 1]are an error (#60). -
rng_uniform(),rng_normal(),rng_integer()andrng_bits()sample; with a vector of keys they return one column per key. -
rng_uniform()andrng_normal()takeoffset:rng_normal(key, n, offset = k)returns positionsktok + n - 1, exactlyrng_normal(key, k + n)[(k + 1):(k + n)], so a long simulation can draw its next batch, or resume from a checkpoint, without the earlier values.offset + nmay not exceed 2^53, checked exactly (#57). -
rng_uniform()accepts any finite bounds: whenmax - minexceeds the largest double, draws are scaled on halved bounds and doubled, exactly, instead of all beingInf; other bounds keep their stream (#57). -
rng_lazy_uniform()andrng_lazy_normal()return lazy vectors (ALTREP): created instantly, with each value computed only when read and exactly equal to the matching sampler’s.sum()over 1e9 lazy normals runs in the memory R itself uses; saving one stores only the key and arguments. Arithmetic and C code that asks for the data pointer fill the vector once. For scattered reads, key it with thephilox4x64engine. Scans over a lazy vector respond to an interrupt, sosum()over 1e12 lazy values can be stopped (#57). Each lazy vector’s cache is at most one engine chunk and never longer than the vector, so a short one costs bytes, not 40 KB (#60). -
rng_normal(method = "mcfarland")samples with McFarland’s (2016) modified ziggurat, whose common case needs no table comparison. Large fills run 1.15-1.25x faster than the default method on x86_64 and 1.05-1.10x on arm64. It is a separate, equally reproducible stream; the default method is unchanged. -
rng_threads()caps OpenMP use;rng_simd()reports or disables the AVX2 path. Neither changes any value.
Performance
- The
xoshiro256ppengine vectorises its generator on x86_64 (AVX2, chosen at run time) and on arm64 (NEON), and converts uniforms in registers without a second pass. Output is identical with the vector path on or off, whichrng_simd()can switch to check. - The ziggurat’s rare slow path is compiled as plain out-of-line code (fdlibm
exp()/log1p()as calls, nocoldattribute): 7-9% faster normals on a Xeon 6973P and 15-19% with Apple clang, same values. - Large fills use OpenMP threads where R was built with it;
rng_threads()caps them, and the values do not depend on the thread count. On macOS, where R does not set OpenMP flags,configurelinks the OpenMP runtime that R ships, as CRAN’s macOS binaries of data.table do, and falls back to one thread when the OpenMP headers are missing. - In a forked child (
parallel::mclapply(),mcparallel()) zurand’s fills run on one thread. GNU OpenMP’s thread pool does not survivefork(), and a child that entered a parallel region after its parent had used one hung (#122). - On Linux kernels with transparent huge pages in
madvisemode, output vectors of 4 MiB and more are filled on 2 MiB pages, which about halves the time of large fills there.options(zurand.hugepages = FALSE)turns the request off.
C API
-
inst/include/zurand.hlets packages thatLinkingTo: zurandfill their own buffers, from their own threads, with exactly the values the R functions return. - API version 2 adds
fill_normal_method(), which takes the normal method asZURAND_NORMAL_ZIGGURATorZURAND_NORMAL_MCFARLAND, the C side ofrng_normal(method =). - API version 3 draws from any position, in two ways.
fill_uniform_at()andfill_normal_at()fill positionsstarttostart + n - 1, so the caller pulls the next piece whenever it wants it.stream_uniform()andstream_normal()run the loop instead: they pour positions0tototal - 1through a small buffer the caller owns, calling a consumer function on each chunk in order, so a long simulation consumes its numbers from cache without the whole stream ever being in memory. Either way the values are those ofrng_uniform()andrng_normal(). - Every entry point reports a bad argument, including a NULL output buffer, as
ZURAND_EINVALrather than crashing the session (#60).