Algorithm for Math.Random
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The Math.random()
function is a critical component of JavaScript and other programming environments, providing developers with access to pseudo-random number generation. This article delves into the technical side of how this function works, its applications, and potential pitfalls.
Understanding Math.random()
The Math.random()
function in JavaScript returns a floating-point, pseudo-random number in the range from 0 (inclusive) to 1 (exclusive). This means any number produced by Math.random()
is no larger than but never equal to 1, and can be 0, although this is exceptionally rare.
Technical Explanation
The Math.random()
function does not produce a truly random number; instead, it generates a number that appears random. This is due to the deterministic nature of pseudo-random number generators (PRNGs), which are algorithms designed to simulate randomness.
Pseudo-Random Number Generators
PRNGs work by taking a fixed initial value, known as the seed, and applying a deterministic set of operations to produce a sequence of numbers. Every time the same seed is used, the same sequence of numbers is generated, which is why the numbers are not truly random.
Common Algorithms
The specific algorithm behind Math.random()
can vary depending on the JavaScript engine but is typically one of several well-known PRNG algorithms, such as:
- Linear Congruential Generator (LCG): This basic and widely used algorithm defines each new number using a simple formula:where:
- is the current number,
- is the multiplier,
- is the increment, and
- is the modulus.
- Mersenne Twister: A more robust algorithm which generates a larger period and more evenly distributed results.
Generating Random Numbers in Different Ranges
While Math.random()
only returns values between 0 and 1, it is often necessary to generate random numbers within different ranges or types (e.g., integers).
Example: Random Integer
To generate a random integer between two values, you can manipulate the output of Math.random()
as follows:
- Predictability: As PRNGs are deterministic, they are inherently predictable if the algorithm and seed are known.
- Periodicity: Every PRNG has a period, at which point the sequence of numbers will repeat. This period must be long enough to avoid any noticeable repetition during typical usage.
Related reading
- Algorithm for maximizing coverage of rectangular area with scaling tiles
- Algorithm for maximum non-dominated set
- Algorithm for maze/labyrinth generation with no dead ends?
- Algorithm for merging short lists into a long vector
- Algorithm for merging two max heaps?
- Algorithm for N-way merge
- Algorithm for neon glow graphics programming
- Algorithm for nice graph labels for time/date axis?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.