gpurng — Control the independent GPU random stream.
gpurng queries, seeds, and restores RunMat's session GPU random stream independently of the CPU rng stream.
Syntax
s = gpurng()
s = gpurng(seed|generator|option|state)
s = gpurng(seed,generator)Inputs
seed, generator, option, or state- Type
Any- Required
- Yes
- Default
- —
- Description
- Nonnegative seed, generator, default/shuffle option, or prior snapshot.
seed- Type
NumericScalar- Required
- Yes
- Default
- —
- Description
- Nonnegative integer seed below 2^32.
generator- Type
StringScalar- Required
- Yes
- Default
- —
- Description
- GPU random generator algorithm.
Returns
s- Type
Any- Description
- Previous GPU stream snapshot with Type, Seed, and State fields.
Errors
RunMat:gpurng:InvalidArgument- When
- A seed or opaque snapshot is malformed.
- Message
gpurng: invalid argument
RunMat:gpurng:GeneratorUnsupported- When
- A documented generator has no implemented request-bound route.
- Message
gpurng: generator is not supported by this runtime
Independent GPU random state
gpurng queries or changes the session GPU random stream used by GPU output forms of rand, randi, randn, and randperm. rng controls the separate ordinary CPU stream; seeding one does not reseed the other. Automatic placement of an ordinary CPU random call must not change its stream or draw order.
gpurng("default") selects Threefry4x64_20 with seed zero. A numeric seed restarts the current GPU generator; gpurng("shuffle") chooses a fresh seed. The returned scalar structure records the previous Type, Seed, and opaque State, and a prior returned snapshot can be restored.
Generator availability and execution
The documented GPU algorithms are Threefry, Philox, and CombRecursive. This RunMat implementation currently executes Threefry; requests for the other algorithms fail before changing the stream. They are not aliases of Threefry. Snapshot bytes are RunMat-owned state, not a claim of byte-for-byte compatibility with MATLAB's opaque state encoding.
A request-bound GPU child range determines each output independently of execution placement. Supported provider forms can generate on the device; other admitted forms may compute from the GPU child stream on the host and restore the requested GPU residency through the exact owner. An operational provider failure after effects is terminal, not a signal to draw again on another route.
Threefry words and normal Box-Muller inputs follow the same RunMat child mapping across supported providers. Floating transcendental last bits may differ by backend. The default seed and generator identity do not assert MATLAB's undocumented seed-to-counter or distribution-lane mapping.
Examples
Save and restore GPU random state
gpurng('default'); s = gpurng; a = rand(2,2,'gpuArray'); gpurng(s); b = rand(2,2,'gpuArray'); same = isequal(gather(a),gather(b))Expected output:
same is true after the GPU state is restoredSeed CPU and GPU streams separately
rng('default'); gpurng('default'); cpu = rand(1,3); gpu = gather(rand(1,3,'gpuArray'))Expected output:
cpu and gpu are each reproducible from their own defaultsUsing gpurng with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how gpurng changes the result.
Run a small gpurng example, explain the result, then change one input and compare the output.
FAQ
Does rng seed the GPU stream?⌄
No. Use gpurng for GPU random output. rng and gpurng control independent session streams.
Can I restore a GPU stream?⌄
Yes. Capture s = gpurng, then call gpurng(s) to restore that RunMat snapshot.
Are Philox and CombRecursive supported?⌄
They are documented GPU generators, but remain unimplemented in RunMat and are rejected before stream mutation; they are not renamed Threefry streams.
Related Stats functions
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Summary
anova1 · binocdf · binscatter · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · ksdensity · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pca · pdf · prctile · qqplot · quantile · refline · rmse · scatterhist · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest · ttest2 · wblinv
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · nlparci · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Hist
Source you can read
Every RunMat function's source is public. Read exactly how gpurng is executed, line by line, in Rust.
- View the source for gpurng in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.