How to generate a random integer in JavaScript
Generating random integers is essential for dice games, array shuffling, test data generation, random avatar selection, and procedural content creation in JavaScript applications.
With over 25 years of experience in software development and as the creator of CoreUI, I’ve used random integer generation in content selectors, chart element IDs, and demo data generators.
The standard approach is combining Math.random() with Math.floor() to produce pseudorandom whole numbers within a specific range.
Use Math.floor() with Math.random() to generate a random integer within a range.
// 0 to 9 (inclusive)
Math.floor(Math.random() * 10)
// 1 to 10 (inclusive)
Math.floor(Math.random() * (10 - 1 + 1)) + 1
Math.random() returns a float in [0, 1). Multiplying by the range size and flooring produces a whole number. Adding the minimum shifts it to the correct range.
1. Reusable Helper Function
Wrap the formula in a helper so you never miscalculate the range again.
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
randomInt(1, 6) // simulates a die roll: 1–6
randomInt(0, 100) // percentage: 0–100
randomInt(-5, 5) // signed range: -5 to 5
The + 1 inside the formula is what makes both min and max inclusive. Without it, max can never be reached.
2. Random Array Index
To pick a random element from an array, generate an index between 0 and array.length - 1.
const colors = ['red', 'green', 'blue', 'yellow']
const randomIndex = Math.floor(Math.random() * colors.length)
const randomColor = colors[randomIndex]
// e.g. 'blue'
This pattern is common for randomizing avatars, selecting a featured item, or picking a random chart color from a palette.
3. Generating Multiple Random Integers
Use Array.from() to create an array of random integers in one line.
// Five random integers between 1 and 100
const samples = Array.from({ length: 5 }, () => randomInt(1, 100))
// e.g. [42, 7, 91, 23, 56]
Useful for generating test data, chart datasets, or mock API responses during development.
4. Using Math.ceil() and Math.round() Alternatives
Math.floor() is the most common choice, but the other rounding methods produce different range behaviors.
// Math.ceil — range is [1, max] (0 can never appear)
Math.ceil(Math.random() * 10) // 1–10
// Math.round — range is [0, max] but 0 and max have half the probability
Math.round(Math.random() * 10) // 0–10, non-uniform at edges
Stick with Math.floor() for uniform distribution across the full range.
5. Cryptographically Secure Random Integers
Math.random() is a pseudorandom number generator (PRNG) — sufficient for UI and games, but not for security-sensitive use cases like tokens, OTPs, or unique IDs. Use the Web Crypto API instead.
function secureRandomInt(min, max) {
const range = max - min + 1
const bytes = new Uint32Array(1)
crypto.getRandomValues(bytes)
return min + (bytes[0] % range)
}
secureRandomInt(100000, 999999) // cryptographically secure 6-digit code
For generating unique identifiers, crypto.randomUUID() is even simpler:
crypto.randomUUID()
// e.g. '110e8400-e29b-41d4-a716-446655440000'
crypto.getRandomValues() and crypto.randomUUID() are available in all modern browsers and Node.js 15+.
Best Practice Note:
Use Math.floor(Math.random() * (max - min + 1)) + min as your standard formula and wrap it in a named helper for readability.
For security-sensitive applications — tokens, PIN codes, lottery draws — always use crypto.getRandomValues() instead of Math.random(), which is not suitable for cryptographic purposes.
We use these patterns in CoreUI’s demo data generators and chart examples to produce realistic-looking random datasets.



