Skip to main content

Overview

Random distribution functions generate samples from standard probability distributions. All functions support deterministic sampling via setSeed().

Seed Management

setSeed

Set global random seed for deterministic sampling.
function setSeed(seed: number): void
seed
number
required
Random seed value (any finite number)
Example:
import { setSeed, rand } from 'deepbox/random';

setSeed(42);
const a = rand([5]);
setSeed(42);
const b = rand([5]);
// a and b contain identical values

getSeed

Get current random seed.
function getSeed(): number | undefined
Returns
number | undefined
Current seed value or undefined if not set

clearSeed

Clear the current random seed and revert to cryptographically secure randomness.
function clearSeed(): void

Continuous Distributions

uniform

Random samples from continuous uniform distribution.
function uniform(
  low?: number,
  high?: number,
  shape?: Shape,
  opts?: RandomOptions
): Tensor
low
number
Lower boundary (default: 0)
high
number
Upper boundary (default: 1)
shape
Shape
Output shape (default: [])
opts.dtype
DType
Data type: 'float32' or 'float64' (default: 'float32')
opts.device
Device
Device placement
Returns
Tensor
Tensor with values uniformly distributed in [low, high)
Example:
import { uniform } from 'deepbox/random';

const x = uniform(-1, 1, [3, 3]);  // Values between -1 and 1

normal

Random samples from normal (Gaussian) distribution.
function normal(
  mean?: number,
  std?: number,
  shape?: Shape,
  opts?: RandomOptions
): Tensor
mean
number
Mean of distribution (default: 0)
std
number
Standard deviation (default: 1)
shape
Shape
Output shape (default: [])
opts
RandomOptions
Options (dtype, device)
Returns
Tensor
Tensor with normally distributed values
Uses Box-Muller transform internally. All values are finite. Example:
import { normal } from 'deepbox/random';

const x = normal(0, 2, [100]);  // Mean 0, std 2

exponential

Random samples from exponential distribution.
function exponential(scale?: number, shape?: Shape, opts?: RandomOptions): Tensor
scale
number
Scale parameter (1/lambda, default: 1, must be > 0)
shape
Shape
Output shape (default: [])
opts
RandomOptions
Options (dtype, device)
Returns
Tensor
Tensor with exponentially distributed values (all positive)
Mean = scale, variance = scale². Uses inverse transform sampling. Example:
import { exponential } from 'deepbox/random';

const x = exponential(2, [100]);

gamma

Random samples from gamma distribution.
function gamma(
  shape_param: number,
  scale?: number,
  shape?: Shape,
  opts?: RandomOptions
): Tensor
shape_param
number
required
Shape parameter (k, must be > 0)
scale
number
Scale parameter (theta, default: 1, must be > 0)
shape
Shape
Output shape (default: [])
opts
RandomOptions
Options (dtype, device)
Returns
Tensor
Tensor with gamma distributed values (all positive)
Mean = shape_param * scale. Uses Marsaglia and Tsang’s method (2000). Example:
import { gamma } from 'deepbox/random';

const x = gamma(2, 2, [100]);

beta

Random samples from beta distribution.
function beta(
  alpha: number,
  beta_param: number,
  shape?: Shape,
  opts?: RandomOptions
): Tensor
alpha
number
required
Alpha parameter (must be > 0)
beta_param
number
required
Beta parameter (must be > 0)
shape
Shape
Output shape (default: [])
opts
RandomOptions
Options (dtype, device)
Returns
Tensor
Tensor with values in the open interval (0, 1)
Mean = alpha / (alpha + beta). Uses ratio of two gamma distributions. Example:
import { beta } from 'deepbox/random';

const x = beta(2, 5, [100]);

Discrete Distributions

binomial

Random samples from binomial distribution.
function binomial(
  n: number,
  p: number,
  shape?: Shape,
  opts?: RandomOptions
): Tensor
n
number
required
Number of trials (non-negative integer)
p
number
required
Probability of success (in [0, 1])
shape
Shape
Output shape (default: [])
opts.dtype
DType
Data type: 'int32' or 'int64' (default: 'int32')
opts.device
Device
Device placement
Returns
Tensor
Tensor with number of successes in range [0, n]
Generates number of successes in n independent Bernoulli trials. Example:
import { binomial } from 'deepbox/random';

const x = binomial(10, 0.5, [100]);  // 10 coin flips, 100 times

poisson

Random samples from Poisson distribution.
function poisson(lambda: number, shape?: Shape, opts?: RandomOptions): Tensor
lambda
number
required
Expected number of events (rate, must be >= 0)
shape
Shape
Output shape (default: [])
opts.dtype
DType
Data type: 'int32' or 'int64' (default: 'int32')
opts.device
Device
Device placement
Returns
Tensor
Tensor with Poisson distributed values
Uses Knuth’s method for lambda < 30, transformed rejection for lambda >= 30. Example:
import { poisson } from 'deepbox/random';

const x = poisson(5, [100]);  // Rate = 5 events

Basic Functions

rand

Random values in half-open interval [0, 1).
function rand(shape: Shape, opts?: RandomOptions): Tensor
shape
Shape
required
Output shape
opts
RandomOptions
Options (dtype, device)
Returns
Tensor
Tensor with values uniformly distributed in [0, 1)
Example:
import { rand } from 'deepbox/random';

const x = rand([2, 3]);  // 2x3 matrix of random values

randn

Random samples from standard normal distribution.
function randn(shape: Shape, opts?: RandomOptions): Tensor
shape
Shape
required
Output shape
opts
RandomOptions
Options (dtype, device)
Returns
Tensor
Tensor with normally distributed values (mean=0, std=1)
Example:
import { randn } from 'deepbox/random';

const x = randn([2, 3]);  // 2x3 matrix of normal random values

randint

Random integers in half-open interval [low, high).
function randint(low: number, high: number, shape: Shape, opts?: RandomOptions): Tensor
low
number
required
Lowest integer (inclusive)
high
number
required
Highest integer (exclusive)
shape
Shape
required
Output shape
opts.dtype
DType
Data type: 'int32' or 'int64' (default: 'int32')
opts.device
Device
Device placement
Returns
Tensor
Tensor with integers uniformly distributed in [low, high)
Example:
import { randint } from 'deepbox/random';

const x = randint(0, 10, [5]);  // 5 random integers from 0 to 9

Build docs developers (and LLMs) love