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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
mu | Any | Yes | — | Mean parameter. |
sigma | Any | Yes | — | Standard deviation parameter (must be >= 0). |
sz | Any | Yes | — | Size scalar or size vector argument. |
sz | Any | Variadic | — | Dimension extents for output shape. |
Returns
| Name | Type | Description |
|---|---|---|
r | NumericArray | Random sample array from normal distribution. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:normrnd:SigmaMustBeNonnegative | sigma is negative. | normrnd: sigma must be non-negative |
RunMat:normrnd:InvalidArgument | Input parameters or size arguments are missing or malformed. | normrnd: invalid argument |
RunMat:normrnd:Internal | Internal conversion/allocation/provider decode fails. | normrnd: internal operation failed |
How normrnd works
normrnd(mu, sigma)returns a scalar double drawn fromN(mu, sigma^2).normrnd(mu, sigma, n)returns ann × nmatrix of samples.normrnd(mu, sigma, m, n)returns anm × nmatrix of samples.normrnd(mu, sigma, sz)accepts a size vector and returns an array with shapesz.muandsigmamust be scalar numeric values.sigmamust be non-negative;normrnderrors ifsigma < 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.
Related Stats functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how normrnd is executed, line by line, in Rust.
- View the source for normrnd in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.