The Math.random()
method returns a pseudo-random floating-point number between 0
(inclusive) and 1
(exclusive).
Math.random(); // Example: 0.423512837
You can use Math.random()
with Math.floor()
to create random integers in a range.
Math.floor(Math.random() * 10) + 1
Math.floor(Math.random() * 100)
Math.floor(Math.random() * (max - min + 1)) + min
// Random number between 1 and 10 let num = Math.floor(Math.random() * 10) + 1; console.log(num); // Between 50 and 100 let custom = Math.floor(Math.random() * (100 - 50 + 1)) + 50; console.log(custom);
// Roll a 6-sided die let roll = Math.floor(Math.random() * 6) + 1; console.log("Dice roll:", roll);
Math.random()
is not cryptographically secure. Use crypto.getRandomValues()
for secure randomness.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!