Random Number Generator Formula

Mathematics & Education Formula • Published on November 03, 2026 • Last updated November 03, 2026

Calculator: Random Number Generator

Mathematical Equation

$$P(X = k) = \frac{1}{b - a + 1} \quad \Big| \quad X = a + \lfloor U \times (b - a + 1) \rfloor \quad \Big| \quad \mathbb{E}[X] = \frac{a + b}{2} \quad \Big| \quad \operatorname{Var}(X) = \frac{(b - a + 1)^2 - 1}{12}$$

Variable Definitions

a

Inclusive lower limit (minimum integer bound)

b

Inclusive upper limit (maximum integer bound)

N

Total count of discrete outcomes: N = b - a + 1

k

Any individual integer outcome within the interval [a, b]

P(X = k)

Probability of selecting integer k on a single unbiased draw

U

Standard continuous uniform random variable on the half-open interval [0, 1)

E[X]

Theoretical expected value (arithmetic mean) across all possible outcomes

Var(X)

Theoretical population variance measuring dispersion of the distribution

Detailed Explanation

In-Depth Guide

Key Concept

A Random Number Generator (RNG) draws values from a discrete uniform distribution where every integer within an inclusive range [a, b] has an identical, unbiased probability of occurrence: P(X = k) = 1 / (b - a + 1). Modern cryptographic generators (CSPRNG) gather entropy from system hardware noise (thermal jitter, clock interrupts) to generate continuous random floats U in [0, 1), which are scaled into discrete integer buckets using X = a + floor(U × (b - a + 1)).

Modulo bias elimination algorithms reject incomplete boundary cycles, guaranteeing that no integer receives unfair statistical preference. When sampling multiple unique numbers without replacement (such as classroom rosters or raffle drawings), the Fisher-Yates shuffle algorithm is applied with time complexity O(N) to ensure combinatorial fairness without duplicated picks.

How to Calculate: Step-by-Step

1. Inclusive Range Bounds Determination: a. Identify lower limit $a$ and upper limit $b$. b. Verify $b \ge a$ (or swap bounds if $a > b$). c. Compute total discrete possible outcomes: $N = b - a + 1$. 2. Unbiased Random Integer Generation: a. Collect cryptographically secure hardware entropy to produce continuous uniform float $U \in [0, 1)$. b. Scale the float by the interval span: $\text{Scaled} = U \times (b - a + 1)$. c. Apply floor truncation and offset by lower bound: $X = a + \lfloor \text{Scaled} \rfloor$. d. Eliminate modulo bias by rejecting entropy bitstrings exceeding the largest integer multiple of $N$. 3. Sampling With vs Without Replacement: a. With Replacement (Independent Draws): Repeat step 2 for each draw $c \in [1, m]$. b. Without Replacement (Unique Numbers / Raffles): Shuffle integer pool using the Fisher-Yates algorithm in $\mathcal{O}(N)$ linear time to draw $m$ distinct winners. 4. Statistical Metrics Calculation: a. Expected Mean: $\mathbb{E}[X] = \frac{a + b}{2}$ b. Distribution Variance: $\operatorname{Var}(X) = \frac{(b - a + 1)^2 - 1}{12}$ c. Standard Deviation: $\sigma = \sqrt{\operatorname{Var}(X)}$

Worked Calculation Example

Let's walk through drawing 3 fair, unique numbers for a classroom of 40 students or a 40-ticket raffle: 1. Range Bounds & Probability: - Minimum Student Roll: $a = 1$ - Maximum Student Roll: $b = 40$ - Total Possible Outcomes: $N = 40 - 1 + 1 = 40$ - Single Pick Likelihood: $$P(X = k) = \frac{1}{40} = 0.025 = 2.5\%$$ 2. Expected Value & Variance: - Expected Mean: $$\mathbb{E}[X] = \frac{1 + 40}{2} = 20.5$$ - Variance: $$\operatorname{Var}(X) = \frac{40^2 - 1}{12} = \frac{1600 - 1}{12} = \frac{1599}{12} = 133.25$$ - Standard Deviation: $$\sigma = \sqrt{133.25} \approx 11.543$$ 3. Simulated Draw (Without Replacement / Unique Winners): - Draw 1: Entropy float $U_1 = 0.6842 \implies 1 + \lfloor 0.6842 \times 40 \rfloor = 1 + 27 = 28$ (1st Winner: Student #28) - Draw 2: Entropy float $U_2 = 0.1035 \implies 1 + \lfloor 0.1035 \times 40 \rfloor = 1 + 4 = 5$ (2nd Winner: Student #5) - Draw 3: Entropy float $U_3 = 0.9412 \implies 1 + \lfloor 0.9412 \times 40 \rfloor = 1 + 37 = 38$ (3rd Winner: Student #38) - Result Set: [28, 5, 38] with sample mean $(28 + 5 + 38) / 3 = 23.67$.

Calculate Random Number Generator Formula Instantly

Skip manual arithmetic — calculate your own values with our free interactive tool.

Open Random Number Generator

Common Use Cases

  • Fair classroom student cold calling and presentation order selection
  • Raffle, sweepstakes, and door prize drawing with guaranteed unique winners
  • Tabletop RPG dice simulations (D6, 2D6, D20, D100) and board game mechanics
  • Random team and project group generation for classrooms and hackathons
  • Cryptographic session key generation, lottery number picks, and monte carlo simulations

Frequently Asked Questions

In a discrete uniform distribution, every integer outcome between lower bound a and upper bound b has an identical probability P(X = k) = 1 / (b - a + 1). No single number has higher odds than any other, ensuring equal likelihood across all participants.

Modulo bias occurs when a random integer from a large range (e.g., 0 to 2^32 - 1) is mapped to a smaller range using simple modulo arithmetic (rand % N). Because 2^32 is rarely evenly divisible by N, smaller remainders appear slightly more often than larger ones. Cryptographic RNGs eliminate this by rejecting and re-rolling values outside the highest clean multiple of N.

A Pseudorandom Number Generator (PRNG) uses deterministic math formulas starting from an initial seed; if the seed is discovered, all future numbers can be predicted. A Cryptographically Secure PRNG (CSPRNG) continuously gathers physical entropy (hardware noise, CPU clock jitter) to generate outputs that are mathematically impossible to predict or reverse-engineer.

Sampling without replacement ensures that once an integer is drawn, it is excluded from all subsequent picks in that draw cycle. Algorithms like the Fisher-Yates shuffle achieve this in linear time O(N), guaranteeing that all drawn raffle winners or classroom team members are distinct without duplicated entries.

What do you need to work out next?

Search 276+ free tools by name, or pick a category below. Every one runs instantly in your browser — no signup, nothing to install.

More Generators
AI ADVISOR & COACH

Ask AI Advisor

Ask AI