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
mu- Type
Any- Required
- Yes
- Description
- Mean parameter.
sigma- Type
Any- Required
- Yes
- Description
- Standard deviation parameter (must be >= 0).
sz- Type
Any- Required
- Yes
- Description
- Size scalar or size vector argument.
sz- Type
Any- Required
- Variadic
- Description
- Dimension extents for output shape.
Returns
r- Type
NumericArray- Description
- Random sample array from normal distribution.
Errors
RunMat:normrnd:SigmaMustBeNonnegative- When
- sigma is negative.
- Message
normrnd: sigma must be non-negative
RunMat:normrnd:InvalidArgument- When
- Input parameters or size arguments are missing or malformed.
- Message
normrnd: invalid argument
RunMat:normrnd:Internal- When
- Internal conversion/allocation/provider decode fails.
- Message
normrnd: internal operation failed
Behavior
normrnd(mu, sigma) returns a scalar 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.
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.
sigma must be non-negative; normrnd errors if sigma < 0.
Any single mu or sigma selects single output; otherwise output is double. A resident data parameter selects the exact owner for class-preserving restoration. Unsupported automatic restoration can keep the host output; explicit gpuArray intent requires restoration.
Execution and residency
Sampling currently uses the shared host normal generator, then restores a resident data parameter result through its exact owning provider. No random_normrnd provider hook is invoked by this implementation. Restoration validates the requested single/double storage class, shape, owner and non-aliasing; typed pre-effect unsupported automatic restoration may keep the host result, while explicit gpuArray intent requires successful restoration. Host parameters do not opportunistically select a global provider.
Random state and fusion
Random generation and RNG state control are stateful boundaries and are not fused into elementwise kernels.
Storage precision
Supported sample/storage class declaration: f32. See the distribution, integer-extension, and residency rules above.
Storage precision
Supported sample/storage class declaration: f64. See the distribution, integer-extension, and residency rules above.
Current implementation limits
The implemented distribution parameters mu and sigma are scalar. Array-valued distribution parameters are not implemented.
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])Select single output from a single data parameter
rng(0); R = normrnd(single(2),single(0),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 · gpurng · lhsdesign · mvnrnd · 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 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 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.