๐ŸŽฒ

Random

Sources
Run the live example Free ยท no sign-up ยท runs in your browser

The RandomNumber block generates pseudo-random scalar numbers drawn from either a uniform or a Gaussian (normal) statistical distribution. It is widely used in control system design, signal processing, and robotics to simulate sensor measurement noise, unmodeled environmental disturbances, stochastic wind gusts, and Monte Carlo tolerance variations. The block can generate fresh random seeds on every run or utilize a fixed repeatable seed for deterministic regression testing.

Mathematical Model

The output sequence y(t)y(t) follows the chosen probability distribution:

y(t)โˆผ{U(a,b),distribution=uniformN(ฮผ,ฯƒ2),distribution=gaussiany(t) \sim \begin{cases} \mathcal{U}(a, b), & \text{distribution} = \text{uniform} \\ \mathcal{N}(\mu, \sigma^2), & \text{distribution} = \text{gaussian} \end{cases}

For the Uniform distribution over interval [a,b][a, b], where aa is minimum and bb is maximum:

p(y)=1bโˆ’a,aโ‰คyโ‰คbp(y) = \frac{1}{b - a}, \quad a \le y \le b

The expected value is E[y]=a+b2\mathbb{E}[y] = \frac{a + b}{2} and the variance is Varโก(y)=(bโˆ’a)212\operatorname{Var}(y) = \frac{(b - a)^2}{12}.

For the Gaussian (normal) distribution with expected value ฮผ\mu (mean) and standard deviation ฯƒ\sigma (stdDev):

p(y)=1ฯƒ2ฯ€expโก(โˆ’(yโˆ’ฮผ)22ฯƒ2)p(y) = \frac{1}{\sigma \sqrt{2\pi}} \exp\left(-\frac{(y - \mu)^2}{2\sigma^2}\right)

Gaussian samples are computed via the Box-Muller transform from two independent uniform variates u1,u2โˆˆ(0,1)u_1, u_2 \in (0, 1):

y=ฮผ+ฯƒโˆ’2lnโกu1cosโก(2ฯ€u2)y = \mu + \sigma \sqrt{-2 \ln u_1} \cos(2\pi u_2)

Inputs & Outputs

Direction ID Label Type Status
← Out out Out number Output

Parameters

Parameters Label Type Default Description
distribution Distribution select uniform
minimum Minimum number 0
maximum Maximum number 1
mean Mean number 0
stdDev Std Dev number 1
randomSeed New Seed Every Run boolean true Draw a fresh seed at the start of every run, so each run gives different numbers. Switch off to repeat a run exactly with a fixed seed.
seed Seed number 12345 Fixed starting point of the sequence: the same seed gives the same numbers every run.

Usage Examples

Example 1: Additive White Gaussian Noise (AWGN) in IMU Gyroscope Measurement

In an attitude-and-heading reference system (AHRS), real rate gyroscopes suffer from wideband electronic thermal noise. Configuring distribution = gaussian, mean = 0.0, stdDev = 0.015 rad/s, and randomSeed = false with seed = 42 injects realistic, repeatable sensor noise into the pitch rate signal. Summing this noise into an Extended Kalman Filter (EKF) allows exact benchmarking of filter tuning and noise rejection.

Example 2: Stochastic Bounded Wind Turbulence on an Autonomous Drone

Simulating stochastic wind speed variations affecting quadrotor position hold. Setting distribution = uniform, minimum = -3.0 m/s, maximum = 3.0 m/s, and randomSeed = true injects unpredictable bounded gusts into the aerodynamic drag equation, testing position controller disturbance rejection under non-deterministic environmental conditions.

Remarks & Best Practices

  • Deterministic Reproducibility: Setting randomSeed = false with a specific seed ensures identical trajectories across repeated simulation runs, automated CI/CD unit tests, and parameter optimization routines.
  • Evaluation Rate Across Solver Substeps: The block draws a fresh pseudo-random number at each discrete simulation step. In multi-stage ODE solvers (such as RK4 or TR-BDF2), internal solver substeps hold the sampled value constant across intermediate stages to maintain solver numerical convergence.
  • Continuous White Noise Scaling: For continuous white noise modeling with spectral power density S0S_0 and simulation sample time TsT_s, scale the discrete standard deviation parameter as ฯƒ=S0/Ts\sigma = \sqrt{S_0 / T_s}.
  • High-Performance WASM Engine Execution: Implemented in WebAssembly using a 64-bit linear congruential and Box-Muller algorithm, imposing near-zero computational overhead even when executing thousands of evaluations per second.

Related Components

  • SignalGenerator: Deterministic periodic waveforms (sine, square, triangle, sawtooth).
  • Constant: Fixed, non-fluctuating reference values.
  • Sum: Combines deterministic control signals with additive stochastic noise.
  • Quantizer: Simulates deterministic ADC quantization discretization steps.