Random number generation and probability distributions
The Random module provides comprehensive random number generation capabilities with support for multiple probability distributions, seeded reproducibility, and tensor-based sampling. All functions work seamlessly with Deepbox tensors.
import { setSeed, clearSeed, getSeed, rand } from 'deepbox/random';// Set seed for reproducible resultssetSeed(42);const a = rand([3]);// Reset seed to get same valuessetSeed(42);const b = rand([3]);// a and b are identicalconsole.log(a); // [0.374, 0.950, 0.731]console.log(b); // [0.374, 0.950, 0.731]// Get current seedconst currentSeed = getSeed(); // 42// Clear seed to use crypto RNGclearSeed();const c = rand([3]); // Non-deterministic
import { rand } from 'deepbox/random';// Random values in [0, 1)const x = rand([3, 4]); // 3x4 matrix// With specific dtypeconst y = rand([5], { dtype: 'float64' });
import { uniform } from 'deepbox/random';// Random values in custom rangeconst x = uniform(-1, 1, [3, 3]); // Values in [-1, 1)// Random weights between 0 and 0.1const weights = uniform(0, 0.1, [10, 20]);
import { randn } from 'deepbox/random';// Standard normal (mean=0, std=1)const z = randn([3, 3]);// Generate noise for regularizationconst noise = randn([100, 10]).mul(0.1); // std=0.1
import { normal } from 'deepbox/random';// Normal distribution with custom mean and stdconst x = normal(5, 2, [1000]); // mean=5, std=2// Generate data with specific statisticsconst data = normal(100, 15, [50]); // mean=100, std=15
import { binomial } from 'deepbox/random';// Number of successes in n trialsconst heads = binomial(10, 0.5, [100]); // 10 coin flips, 100 times// Customer conversions (20 visitors, 15% conversion rate)const conversions = binomial(20, 0.15, [365]); // Daily for a year
import { poisson } from 'deepbox/random';// Number of events in fixed intervalconst events = poisson(5, [100]); // Average 5 events per interval// Website visits per hour (average 30)const visits = poisson(30, [24]); // 24 hours
import { exponential } from 'deepbox/random';// Time between eventsconst waitTimes = exponential(2, [100]); // Mean wait time = 2// Server response timesconst responseTimes = exponential(0.5, [1000]);
import { beta } from 'deepbox/random';// Beta distribution (values in [0, 1])const x = beta(2, 5, [100]); // alpha=2, beta=5// Model proportions or probabilitiesconst proportions = beta(3, 3, [1000]);