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));
}

Learn how random numbers work in JavaScript

Understand the difference between Math.random(), integer ranges, and cryptographically secure random values, then copy the examples straight into your own code.

Read the step-by-step random number guide