unifrnd — Generate uniform random samples on interval [a, b) with MATLAB-compatible size handling.
unifrnd(a, b, ...) draws pseudorandom samples from a continuous uniform distribution over [a, b). It follows MATLAB-compatible parameter broadcasting and optional size-argument forms.
Syntax
r = unifrnd(a, b)
r = unifrnd(a, b, sz)
r = unifrnd(a, b, sz1, sz2, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
a | Any | Yes | — | Lower bound parameter. |
b | Any | Yes | — | Upper bound parameter (must be > a). |
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 uniform distribution. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:unifrnd:LowerBoundMustBeLessThanUpperBound | a is greater than or equal to b. | unifrnd: a must be less than b |
RunMat:unifrnd:InvalidArgument | Input parameters or size arguments are missing or malformed. | unifrnd: invalid argument |
RunMat:unifrnd:Internal | Internal conversion/allocation/provider decode fails. | unifrnd: internal operation failed |
How unifrnd works
unifrnd(a, b)returns a scalar double drawn fromUniform(a, b).unifrnd(a, b, n)returns ann × nmatrix of samples.unifrnd(a, b, m, n)returns anm × nmatrix of samples.unifrnd(a, b, sz)accepts a size vector and returns an array with shapesz.aandbmust be scalar numeric values.amust be less thanb;unifrnderrors ifa >= b.- All outputs are double precision regardless of input type.
Examples
Single sample from Uniform(2, 5)
rng(0);
r = unifrnd(2, 5)Matrix of random values between -1 and 1
rng(0);
X = unifrnd(-1, 1, 2, 3)Monte Carlo samples for uncertain input bounds
rng(0);
low = 10; high = 20;
samples = unifrnd(low, high, 1, 5)Specifying dimensions with a size vector
rng(0);
T = unifrnd(0, 100, [2 3])Using unifrnd with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how unifrnd changes the result.
Run a small unifrnd example, explain the result, then change one input and compare the output.
FAQ
Are the bounds inclusive?⌄
RunMat generates samples on the half-open interval [a, b): values can be equal to a, but are always strictly less than b.
What is the workaround if unifrnd is unavailable?⌄
unifrnd(a, b) is mathematically equivalent to a + (b - a) * rand(). For arrays, use a + (b - a) * rand(sz) with the same requested size.
Why must a be less than b?⌄
A continuous uniform distribution needs a positive-width interval. unifrnd raises an error if a >= b to catch invalid bounds early.
What use cases does unifrnd support?⌄
Common uses include Monte Carlo sampling, randomized initial conditions, parameter sweeps over bounded ranges, simulation inputs, and continuous random perturbations.
Does unifrnd 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 unifrnd to seed the global generator.
Related Stats functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how unifrnd is executed, line by line, in Rust.
- View the source for unifrnd 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.