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));
}Online Random Number Generator (No Coding Required)
This tool lets you generate random numbers instantly without writing any JavaScript code. Just set your range (Min/Max), choose how many numbers you need, and click generate. It's perfect for lotteries, contests, raffles, or scientific sampling.
How to Generate Random Numbers in JavaScript
1The Basic Method: Math.random()
The built-in Math.random() function is the starting point for most random operations in JavaScript. 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
To get a random integer between a minimum and maximum value (inclusive), you need to scale the result of Math.random() and then floor it. This is the most common copy-paste snippet developers need.
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! 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
🎲 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
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