randn — Generate standard normal random numbers in MATLAB and RunMat.
randn draws pseudorandom samples from the standard normal distribution (μ = 0, σ = 1). Size forms and 'like' behavior follow MATLAB semantics.
Syntax
A = randn()
A = randn(n)
A = randn(size_vector)
A = randn(m, n, ...)
A = randn(..., typename)
A = randn(..., "like", prototype)Inputs
n- Type
SizeArg- Required
- Yes
- Description
- Square size.
size_vector- Type
SizeArg- Required
- Yes
- Description
- Size vector defining output dimensions.
dims- Type
SizeArg- Required
- Variadic
- Description
- Dimension sizes.
typename- Type
StringScalar- Required
- No
- Default
"double"- Description
- Class override ('double'|'single'|'gpuArray').
like_kw- Type
StringScalar- Required
- Yes
- Default
"like"- Description
- Like keyword.
prototype- Type
LikePrototype- Required
- Yes
- Description
- Prototype array used for class/device.
Returns
A- Type
NumericArray- Description
- Normal random array (mean 0, variance 1).
Errors
- When
- The 'like' keyword is provided without a prototype argument.
- Message
randn: expected prototype after 'like'
- When
- A trailing option string is not supported.
- Message
randn: unrecognised option
- When
- A prototype type cannot be used for randn(..., 'like', prototype).
- Message
randn: unsupported prototype
- When
- Dimension arguments fail numeric/shape parsing.
- Message
randn: dimension arguments must be numeric and nonnegative
Behavior
randn() returns a scalar double drawn from 𝒩(0, 1).
randn(n) returns an n × n dense double matrix.
randn(m, n, ...) accepts an arbitrary number of dimension arguments.
randn(sz) accepts a documented row size vector; a column size vector is an independently gated RunMat-mode extension.
All eight integer classes are documented for scalar, separate-dimension, and row-size-vector controls and are decoded structurally from authoritative storage; resident size controls are separately gated RunMat extensions.
randn(___, 'like', A) matches the type, complexity, and residency of A, while ordinary size arguments determine shape and omission of size arguments returns a scalar. The old implicit randn(A) prototype form is not supported.
Complex prototypes yield complex Gaussian samples with independent real and imaginary parts, each having variance 1/2, so the complex sample has unit total variance.
Class specifiers support native 'single' and 'double'; integer and logical output prototypes are rejected before generation.
Execution and residency
Ordinary randn construction does not guarantee GPU residency. RunMat may select an accelerated route for a larger admitted computation. A resident 'like' prototype requests its owning provider and output class; an explicit gpuArray conversion is available when device residency is required.
CPU output consumes the session CPU stream controlled by rng. Explicit GPU or resident-like output consumes the independent GPU child stream controlled by gpurng; a selected provider may generate on device, or an admitted host-mediated route may compute from that same GPU stream and restore the exact GPU owner. Backend transcendental last bits are not promised identical.
Examples
Drawing a single standard normal variate
rng(0);
z = randn()Creating a matrix of Gaussian noise
rng(0);
E = randn(2, 3)Specifying dimensions with a size vector
rng(0);
shape = [2 2 2];
T = randn(shape)Matching a gpuArray prototype and inspecting sample mean and standard deviation
gpurng(0);
G = gpuArray.zeros(512, 512);
noise = randn(512, 512, 'like', G);
stats = [mean(gather(noise(:))) std(gather(noise(:)))]Generating complex Gaussian samples
rng(0);
z = randn(3, 1, 'like', 1 + 1i)Producing reproducible noise for Monte Carlo tests
rng(0);
samples = randn(1, 5)Using randn with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how randn changes the result.
Run a small randn example, explain the result, then change one input and compare the output.
FAQ
What distribution does randn use?⌄
randn returns samples from the standard normal distribution with mean 0 and standard deviation 1.
How is randn different from rand?⌄
randn draws from a Gaussian distribution, whereas rand draws from the uniform distribution in [0, 1).
How do I control reproducibility?⌄
Use rng for ordinary CPU output and gpurng for explicit GPU or resident GPU-like output; the two streams are independent.
Does randn(___, 'like', A) work with complex prototypes?⌄
Yes. When A is complex, its real and imaginary components are independent normal variates with variance 1/2 each.
What happens if I request 'single' precision?⌄
RunMat returns a native single-precision tensor. A resident Single request uses a matching selected provider or a preselected host-mediated GPU-child route; a provider failure after effects never triggers a redraw.
How does randn behave on the GPU?⌄
GPU output uses the independent gpurng child stream. A selected provider may generate on device; an admitted host-mediated route uses the same child counters and restores GPU residency through the exact owner.
Can I request zero-sized dimensions?⌄
Yes. Any dimension argument equal to zero yields an empty array, matching MATLAB semantics.
Does randn fuse with other operations?⌄
No. Random generation is treated as a sink operation and excluded from fusion planning to preserve statistical properties.
Related Array functions
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how randn is executed, line by line, in Rust.
- View the source for randn 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.