normrnd — Generate normally distributed random numbers in MATLAB and RunMat.
normrnd(mu, sigma, ...) draws pseudorandom samples from a normal distribution with scalar mean mu and standard deviation sigma. Scalar and vector size forms control the output shape.
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 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.- Current MATLAB documents single and double mu, sigma, and size values. Typed-integer and logical inputs are independently gated RunMat extensions; integer parameters must be exactly representable at the binary64 sampling boundary.
- Integer size controls are parsed structurally and do not select output class or residency. Explicit resident size controls are a separate RunMat extension.
sigmamust be non-negative;normrnderrors ifsigma < 0.- Any single mu or sigma selects single output; otherwise output is double. A resident data parameter preserves output residency through its exact owner.
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
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Hist
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.