RunMat
GitHub

normrnd — Generate normally distributed random numbers in MATLAB and RunMat.

normrnd(mu, sigma, ...) draws pseudorandom samples from normal distributions with mean mu and standard deviation sigma. Size forms, broadcasting, and parameter validation follow MATLAB semantics.

Syntax

r = normrnd(mu, sigma)
r = normrnd(mu, sigma, sz)
r = normrnd(mu, sigma, sz1, sz2, ...)

Inputs

NameTypeRequiredDefaultDescription
muAnyYesMean parameter.
sigmaAnyYesStandard deviation parameter (must be >= 0).
szAnyYesSize scalar or size vector argument.
szAnyVariadicDimension extents for output shape.

Returns

NameTypeDescription
rNumericArrayRandom sample array from normal distribution.

Errors

IdentifierWhenMessage
RunMat:normrnd:SigmaMustBeNonnegativesigma is negative.normrnd: sigma must be non-negative
RunMat:normrnd:InvalidArgumentInput parameters or size arguments are missing or malformed.normrnd: invalid argument
RunMat:normrnd:InternalInternal conversion/allocation/provider decode fails.normrnd: internal operation failed

How normrnd works

  • normrnd(mu, sigma) returns a scalar double drawn from N(mu, sigma^2).
  • normrnd(mu, sigma, n) returns an n × n matrix of samples.
  • normrnd(mu, sigma, m, n) returns an m × n matrix of samples.
  • normrnd(mu, sigma, sz) accepts a size vector and returns an array with shape sz.
  • mu and sigma must be scalar numeric values.
  • sigma must be non-negative; normrnd errors if sigma < 0.
  • All outputs are double precision regardless of input type.

Examples

Single sample from N(0, 1)

rng(0);
r = normrnd(0, 1)

Matrix of Gaussian noise with mean 10 and standard deviation 2

rng(0);
X = normrnd(10, 2, 2, 3)

Simulating normally-distributed measurement errors

rng(0);
trueValue = 100;
sigma = 0.5;
measurements = trueValue + normrnd(0, sigma, 1, 5)

Specifying dimensions with a size vector

rng(0);
T = normrnd(5, 1.5, [2 3])

Using normrnd with coding agents

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

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

FAQ

What do mu and sigma mean?

mu is the distribution mean and sigma is the standard deviation. The variance is sigma^2, so normrnd(mu, sigma) draws from N(mu, sigma^2).

What is the workaround if normrnd is unavailable?

normrnd(mu, sigma) is mathematically equivalent to mu + sigma * randn(). For arrays, use mu + sigma * randn(sz) with the same requested size.

Can sigma be zero?

Yes. sigma = 0 is allowed and returns deterministic samples equal to mu. Negative standard deviations are undefined and raise an error.

What use cases does normrnd support?

Common uses include Gaussian noise generation, measurement error simulation, confidence interval experiments, Monte Carlo sampling, and normally-distributed model residuals.

Does normrnd fuse with other operations?

No. Random generation is excluded from fusion planning to preserve statistical properties.

How do I control reproducibility?

Use rng before calling normrnd to seed the global generator.

Random

exprnd · rng · unifrnd

Summary

corrcoef · cov · mode

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how normrnd is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.