RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.1, Nov 17, 2026

RunMat® is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • binornd
    • bootstrp
    • datasample
    • dividerand
    • exprnd
    • gamrnd
    • gpurng
    • lhsdesign
    • mvnrnd
    • normrnd
    • random
    • randsample
    • rng
    • trnd
    • unidrnd
    • unifrnd
    • wblrnd

rng — Configure, query, and restore RunMat's global pseudorandom number generator through MATLAB-shaped forms.

rng queries or reconfigures the host pseudorandom generator used by rand, randn, randi, randperm, and host-backed distribution samplers. RunMat uses MT19937 for the public twister engine. Its normal transform, exact sequences, and state-wire format are not a claim of MATLAB stream interchange compatibility.

Syntax

s = rng()
s = rng(seed)
s = rng(option)
s = rng(state)
s = rng(seed, generator)
s = rng(option, generator)

Inputs

seed
Type
Any
Required
Yes
Description
Non-negative integer seed.
option
Type
StringScalar
Required
Yes
Description
Option token: 'default' or 'shuffle'.
state
Type
Any
Required
Yes
Description
State struct containing Type, optional Seed, and State fields.
generator
Type
StringScalar
Required
Yes
Default
"twister"
Description
Generator token: 'twister'/'default'; 'runmat-lcg-v1' is accepted only in a saved legacy state.
NameTypeRequiredDefaultDescription
seedAnyYes—Non-negative integer seed.
optionStringScalarYes—Option token: 'default' or 'shuffle'.
stateAnyYes—State struct containing Type, optional Seed, and State fields.
generatorStringScalarYes"twister"Generator token: 'twister'/'default'; 'runmat-lcg-v1' is accepted only in a saved legacy state.

Returns

s
Type
Any
Description
RNG state snapshot struct with fields Type, Seed, and State.
NameTypeDescription
sAnyRNG state snapshot struct with fields Type, Seed, and State.

Errors

RunMat:rng:SeedMustBeNonnegative
When
Seed value is negative.
Message
rng: seed must be non-negative
RunMat:rng:GeneratorUnsupported
When
Generator token is unsupported.
Message
rng: generator is not supported
RunMat:rng:StateTypeFieldMissing
When
State struct is missing the Type field.
Message
rng: state struct is missing the 'Type' field
IdentifierWhenMessage
RunMat:rng:SeedMustBeNonnegativeSeed value is negative.rng: seed must be non-negative
RunMat:rng:GeneratorUnsupportedGenerator token is unsupported.rng: generator is not supported
RunMat:rng:StateTypeFieldMissingState struct is missing the Type field.rng: state struct is missing the 'Type' field
RunMat:rng:InvalidArgumentArguments are missing, malformed, or incompatible with supported forms.rng: invalid argument
RunMat:rng:InternalInternal snapshot conversion/allocation/apply fails.rng: internal operation failed
RunMat:rng:ContextRequiredShuffle is called without an invocation RuntimeContext.rng: shuffle requires an invocation context

Behavior

rng() returns a structure describing the current generator (fields Type, Seed, and State).

rng(seed) reads a non-negative integer seed below 2^32 from scalar double or any of the eight native integer classes. Seeds at or above 2^32 are an independently gated RunMat extension.

rng('default') restores the host MT19937 engine with user seed 0. It is reproducible within RunMat; MATLAB sequence and state-wire equivalence are separate conformance obligations.

rng('shuffle') requires an invocation context. A valid unsigned RUNMAT_RNG_SHUFFLE_SEED from its session environment overrides seeding without a Clock read; otherwise the admitted Clock supplies the wall-time duration. The same duration yields the same seed, without ambient clock or address entropy. Missing context or unavailable Clock fails before changing RNG state.

rng(seed, 'twister') selects the host MT19937 generator. RunMat does not claim MATLAB normal-transform or byte-for-byte snapshot interchange compatibility.

rng(S) restores a structure previously returned by RunMat's rng, including the supported legacy LCG snapshot format. Independently constructed native-integer Seed or legacy LCG State fields require the separately gated typed-state extension. The explicit device RNG is independent: rng does not forward host MT19937 state to, or reseed, a device provider.

Execution and residency

rng is host-stream state control, not an accelerator numeric kernel. The explicit device RNG is independent and is managed by gpurng; no host state token is forwarded to a provider.

rng changes only the host generator. It does not synchronize or reseed the explicit device RNG. Automatic tensor placement does not turn a host snapshot into device RNG state, and CPU/GPU random-sequence identity is not claimed.

Random state and fusion

Random generation and RNG state control are stateful boundaries and are not fused into elementwise kernels.

Current implementation limits

RunMat snapshots are not a MATLAB state-wire interchange format, and exact MATLAB random-sequence/normal-transform parity is not established. Independent host/device stream APIs remain distinct.

Examples

Resetting the generator for reproducible simulation runs

rng(0);
u = rand(1, 4);

Saving and restoring RNG state around a computation

rng(0);
s = rng;
rng(1337);
A = randn(2, 2);
rng(s);            % restore original stream
B = randn(2, 2);   % continues from the saved state

Scrambling the generator with the system clock

rng('shuffle');
u = rand(1, 3)

Replay host-generated data after transferring it to the GPU

rng(2024);
G = gpuArray(rand(1, 3));
C = gather(G);
rng(2024);
H = rand(1, 3);

Creating deterministic permutations with rng and randperm

rng(42);
p = randperm(6);
rng(42);
q = randperm(6)

Preserve a wide native seed exactly

seed = bitshift(uint64(1),53) + uint64(1); rng(seed); s = rng; u = rand(1,3); rng(s); again = rand(1,3);

Using rng with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how rng changes the result.

Run a small rng example, explain the result, then change one input and compare the output.

FAQ

What generators does RunMat support?⌄

The public 'twister' spelling selects RunMat's host MT19937 generator. Legacy LCG snapshots remain restorable for backward compatibility. Additional generator APIs and MATLAB sequence/state-wire parity are separate obligations.

What numeric range is valid for seeds?⌄

MATLAB-compatible mode accepts non-negative integer seeds below 2^32. RunMat mode extends the domain through u64; typed values remain exact, and snapshot Seed fields use native uint64 when binary64 would round.

How can I force a specific seed for rng('shuffle') in tests?⌄

Populate RUNMAT_RNG_SHUFFLE_SEED with an unsigned integer in the invocation's frozen session environment. rng('shuffle') uses that override without reading Clock. Without a valid override, an admitted Clock is required; the builtin never consults the process environment, ambient wall clock, or address entropy.

Does rng affect GPU random numbers?⌄

No. The explicit device RNG has independent state managed by gpurng. rng configures the host stream and does not pass MT19937 state to device providers or claim CPU/GPU sequence identity.

How do I restore a saved state structure?⌄

Pass a structure returned by the same RunMat build back to rng: s = rng; ...; rng(s); resumes the exact host stream. A RunMat snapshot is not a MATLAB Twister interchange format; legacy LCG snapshots are also accepted.

What is stored in the State field?⌄

For MT19937, State is a 625-by-1 uint32 tensor: 624 state words followed by RunMat's next-word index. Legacy LCG snapshots use the older two-word representation. Seed is stored as double when exact, or native uint64 when binary64 would round it. Restoring the returned structure preserves the exact supported engine state.

Can I create independent streams?⌄

Not yet. RunMat exposes MATLAB-compatible RandStream APIs in a separate roadmap item; for now rng operates on the single global stream.

Does rng change automatically when I call other random functions?⌄

Sampling consumes and advances the current stream but does not reseed it. Explicit rng calls configure or restore the host stream; provider-specific state is separate. A discarded result can still consume randomness, according to the called sampler's output-count semantics.

Related Stats functions

Random

binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · gpurng · lhsdesign · mvnrnd · normrnd · random · randsample · 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

histc · histcounts · histcounts2

Options

statget · statset

Source you can read

Every RunMat function's source is public. Read exactly how rng is executed, line by line, in Rust.

  • View the source for rng 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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • Behavior
  • Execution and residency
  • Random state and fusion
  • Current implementation limits
  • Examples
  • Resetting the generator for reproducible simulation runs
  • Saving and restoring RNG state around a computation
  • Scrambling the generator with the system clock
  • Replay host-generated data after transferring it to the GPU
  • Creating deterministic permutations with rng and randperm
  • Preserve a wide native seed exactly
  • Using rng with coding agents
  • FAQ
  • Related Stats functions
  • Random
  • Summary
  • Ml
  • Hist
  • Options
  • Source you can read
  • About RunMat