RunMat
GitHub

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

NameTypeRequiredDefaultDescription
aAnyYesLower bound parameter.
bAnyYesUpper bound parameter (must be > a).
szAnyYesSize scalar or size vector argument.
szAnyVariadicDimension extents for output shape.

Returns

NameTypeDescription
rNumericArrayRandom sample array from uniform distribution.

Errors

IdentifierWhenMessage
RunMat:unifrnd:LowerBoundMustBeLessThanUpperBounda is greater than or equal to b.unifrnd: a must be less than b
RunMat:unifrnd:InvalidArgumentInput parameters or size arguments are missing or malformed.unifrnd: invalid argument
RunMat:unifrnd:InternalInternal conversion/allocation/provider decode fails.unifrnd: internal operation failed

How unifrnd works

  • unifrnd(a, b) returns a scalar double drawn from Uniform(a, b).
  • unifrnd(a, b, n) returns an n × n matrix of samples.
  • unifrnd(a, b, m, n) returns an m × n matrix of samples.
  • unifrnd(a, b, sz) accepts a size vector and returns an array with shape sz.
  • a and b must be scalar numeric values.
  • a must be less than b; unifrnd errors if a >= 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.

Random

exprnd · normrnd · rng

Summary

corrcoef · cov · mode

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how unifrnd is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.