JavaScript Random Number Generator
Generate secure, customizable random numbers for browsers & Node.js
With ready-to-use JavaScript code examples
JavaScript Code
// Generate random integer between 1 and 100
const randomNumber = Math.floor(Math.random() * (100 - 1 + 1)) + 1;Security Notice
Math.random() is not cryptographically secure. For passwords, tokens, or security purposes, use crypto.getRandomValues() instead.
Advanced Examples
Cryptographically Secure
// For security-sensitive applications
function getCryptoSecureRandom(min, max) {
const range = max - min + 1;
const randomBuffer = new Uint32Array(1);
crypto.getRandomValues(randomBuffer);
return min + (randomBuffer[0] % range);
}Random Float
// Generate random float
function getRandomFloat(min, max, decimals = 2) {
const random = Math.random() * (max - min) + min;
return Number(random.toFixed(decimals));
}On this page
Online JavaScript Random Number Generator (No Coding Required)
This free JavaScript random number generator tool lets you generate random numbers instantly without writing any code. Whether you need to generate random numbers in JavaScript for testing, lotteries, or scientific sampling, just set your range (Min/Max), choose the quantity, and click generate.
How to Generate Random Numbers in JavaScript
1The Basic Method: Math.random()
The built-in Math.random() function is the essential starting point for any JavaScript random number generator. It returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1).
// Returns a value like 0.123456789...
const random = Math.random();2Generating Random Integers in a Range
When you need to generate random numbers in JavaScript between a minimum and maximum value (inclusive), you must scale the result of Math.random() and then floor it. This is the most common copy-paste snippet developers need for their projects.
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
// The maximum is inclusive and the minimum is inclusive
return Math.floor(Math.random() * (max - min + 1) + min);
}
// Example: Get a random number between 1 and 10
console.log(getRandomInt(1, 10));Security Warning: Passwords & Tokens
Never use Math.random() for security-critical applications! While it is fine for a general JS random number generator, it is not cryptographically secure. For passwords, API keys, or session IDs, always use the Web Crypto API.
Need to generate secure tokens? Try our Random String Generator or UUID Generator.
// ✅ Secure way
const array = new Uint32Array(1);
self.crypto.getRandomValues(array);
console.log(array[0]);Common Use Cases for JavaScript Random Number Generation
🎲 Simulating Dice
Simulate a 6-sided die roll.
Math.floor(Math.random() * 6) + 1🪙 Coin Flip
Return true (Heads) or false (Tails).
Math.random() < 0.5🎯 Random Array Element
Pick a random item from a list.
items[Math.floor(Math.random() * items.length)]🎨 Random Color
Generate a random hex color code.
'#' + Math.floor(Math.random()*16777215).toString(16)Frequently Asked Questions about JS Random Numbers
How do I generate random numbers in JavaScript?
Use Math.random() to generate numbers between 0 and 1, then scale to your desired range. For integers, use Math.floor(Math.random() * (max - min + 1)) + min.
Is Math.random() secure for passwords?
No, Math.random() is not cryptographically secure. For passwords, tokens, or security purposes, use crypto.getRandomValues() instead.
Can I generate unique random numbers?
Yes, you can prevent duplicates by tracking used numbers in an array or Set.
Need a deeper dive?
Check out our comprehensive guide on random number generation, covering everything from basic math to advanced algorithms and seeding.
Read the Full Guide