Skip to content

Random Number Generator Formula and Range Mapping

Review Web Crypto source values, rejection sampling, unbiased inclusive integer mapping, 53-bit decimal fractions, rounding, uniqueness, and sorting.

How do the Random Number Generator formulas work?

This formula page defines the variables used by the Random Number Generator, shows how those values are combined, and explains the order of operations behind the result. Use the worked steps to verify an estimate, compare scenarios, and understand which assumptions or rounding rules can affect the displayed answer.

Formula Overview

The browser supplies unsigned 32-bit random values. Integer mode rejects source values from an uneven remainder interval before modulo mapping. Decimal mode combines two source values into a fraction with 53 bits of resolution and scales it to the entered interval.

Variable Definitions

  • a: entered minimum
  • b: entered maximum
  • s: count of eligible integers
  • r: unsigned 32-bit source value
  • L: rejection limit
  • u: random fraction in [0, 1)
  • x: generated result

Inclusive Integer Span

s=b−a+1s=b-a+1

Adding one includes both endpoints. For 1 through 10, the span is 10 rather than 9.

Rejection-Sampling Threshold

L=⌊232s⌋sL=\left\lfloor\frac{2^{32}}{s}\right\rfloor s

A source value is accepted only when r is below L. Values from L through 2³² - 1 are discarded and replaced. This removes the short interval that would otherwise favor some remainders.

Inclusive Integer Mapping

x=a+(r mod s),r<Lx=a+(r\bmod s),\quad r<L

The accepted remainders range from 0 through s - 1, so the final values range from a through b.

53-Bit Decimal Fraction

The engine takes the upper 27 bits from one source value and the upper 26 bits from another:

u=high×226+low253u=\frac{high\times2^{26}+low}{2^{53}}

This produces a fraction greater than or equal to zero and strictly below one before display rounding.

Decimal Range Mapping and Display

x=a+u(b−a)x=a+u(b-a)

The value is rounded and formatted to the selected 0 to 10 decimal places. Rounding can make a displayed value equal the maximum or make two generated fractions appear identical.

Unique Integers and Result Sorting

In unique integer mode, accepted values already in the set are discarded until the requested count is reached. Sorting is performed only after the list is complete. Decimal mode does not promise unique values.

Worked Integer Mapping

For an inclusive range from 10 to 20, the span is 11. A controlled accepted source value of 0 gives 10 + (0 mod 11) = 10. Source values 1, 2, and 3 map to 11, 12, and 13. These values form the independent test fixture for unique ascending generation.

Assumptions and Boundaries

  • The runtime provides a working Web Crypto `getRandomValues` implementation.
  • Input bounds are -1,000,000,000 through 1,000,000,000.
  • Quantity is limited to 1 through 100.
  • Whole-number endpoints are inclusive.
  • Decimal values are rounded only to the chosen display precision.
  • No seed, reproducibility, certification, or regulated audit record is provided.