PureTools

Random Number Generator: Ranges, Distributions, and Use Cases

PureTools Team· 6 min read
Random Number Generator: Ranges, Distributions, and Use Cases

Random Numbers Are Everywhere

Random numbers power everything from games and simulations to cryptography and scientific research. But not all random numbers are created equal - understanding the difference matters.

Pseudo-Random vs True Random

TypeSourceSpeedUse Case
Pseudo-random (PRNG)Mathematical algorithmVery fastGames, simulations, testing
Cryptographic (CSPRNG)OS entropy poolFastSecurity, tokens, passwords
True random (TRNG)Physical phenomenaSlowLotteries, scientific research

In JavaScript

// Pseudo-random (NOT cryptographically secure)
Math.random() // 0 to 1
Math.floor(Math.random() * 100) + 1 // 1 to 100

// Cryptographically secure
const array = new Uint32Array(1);
crypto.getRandomValues(array);
const secureRandom = array[0]; // 0 to 4,294,967,295

// Random integer in range (secure)
function randomInt(min, max) {
  const range = max - min + 1;
  const array = new Uint32Array(1);
  crypto.getRandomValues(array);
  return min + (array[0] % range);
}

Common Use Cases

  • Games: Dice rolls, card shuffling, procedural generation
  • Security: Password generation, session tokens, nonces
  • Testing: Fuzz testing, random test data, Monte Carlo simulations
  • Statistics: Sampling, bootstrapping, randomized experiments
  • Art: Generative art, procedural textures, music

Distributions Matter

Not all random should be uniform. Different distributions serve different purposes:

  • Uniform: Equal probability across range (dice, lottery)
  • Normal (Gaussian): Bell curve, most values near the mean (heights, test scores)
  • Exponential: Models time between events (server requests, radioactive decay)
  • Poisson: Count of events in a period (calls per hour, website visits)

The Bias Problem

Using modulo (%) to constrain a random number to a range introduces bias when the range doesn't evenly divide the source range. For critical applications, use rejection sampling.

// Biased (avoid for security)
random() % 10  // slightly favors lower numbers

// Unbiased
function unbiasedRandom(max) {
  const limit = Math.floor(0xFFFFFFFF / max) * max;
  let value;
  do {
    const arr = new Uint32Array(1);
    crypto.getRandomValues(arr);
    value = arr[0];
  } while (value >= limit);
  return value % max;
}

Generate random numbers with custom ranges using the PureTools Random Number Generator. Set your min and max values, choose how many numbers to generate, and get cryptographically secure results instantly.