bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript Random

// Returns a random number:
Math.random();

JavaScript Math.random()

Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):

Math.random() always returns a number lower than 1.

JavaScript Random Integers

Math.random() used with Math.floor() can be used to return random integers.

There is no such thing as JavaScript integers.

We are talking about numbers with no decimals here.

// Return a random integer from 0 to 9 (both included):
Math.floor(Math.random() * 10);

Explained

Math.random() returns a floating-point number between 0 (inclusive) and 1 (exclusive).

Example outputs: 0.0, 0.237, 0.9999, but never 1.

Math.random() * 10 gives a range from 0 up to but not including 10.

Example possible results: 0.0, 3.5, 9.99, etc.

  • 3.5 becomes 3
  • 9.99 becomes 9
  • 0.1 becomes 0
// Return a random integer from 0 to 10 (both included):
Math.floor(Math.random() * 11);
// Return a random integer from 0 to 99 (both included):
Math.floor(Math.random() * 100);
// Return a random integer from 0 to 100 (both included):
Math.floor(Math.random() * 101);
// Return a random integer between 1 and 10 (both included):
Math.floor(Math.random() * 10) + 1;

Explained

Math.random() returns a number from 0 (inclusive) up to but not including 1.

Multiplying by 10 gives a number from 0 up to but not including 10.

Adding 1 shifts that range to 1 up to but not including 11.

Math.floor() then rounds down, so you get an integer between 1 and 10.

// Returns a random integer from 1 to 100 (both included):
Math.floor(Math.random() * 100) + 1;

Summary

ExpressionRange fromRange to
Math.random()0<1
Math.random() * 100<10
Math.random() * 1000<100
Math.floor(Math.random() * 10)09

A Proper Random Function

As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes.

This JavaScript function always returns a random integer between min (included) and max (excluded):

Example

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}

This JavaScript function always returns a random integer between min and max (both included):

Example

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}

Previous

JavaScript Map Methods

Next

JavaScript RegExp Flags

This chapter

Overview
37

Lessons

216m

Read time

1. JavaScript Strings2. JavaScript Numbers3. JavaScript Dates4. JavaScript Arrays5. JavaScript Sets6. JavaScript Maps7. JavaScript Math Object8. JavaScript RegExp9. JavaScript String Templates10. JavaScript Number Methods11. JavaScript Date Formats12. JavaScript Array Methods13. JavaScript Set Methods14. JavaScript Map Methods15. JavaScript Random16. JavaScript RegExp Flags17. JavaScript String Methods18. JavaScript Number Properties19. JavaScript Get Date Methods20. JavaScript Array Search21. JavaScript Set Logic22. JavaScript WeakMap23. RegExp Character Classes24. JavaScript String Search25. JavaScript Bitwise Operations26. JavaScript Set Date Methods27. JavaScript Array Sort28. JavaScript WeakSet29. RegExp Meta Characters30. JavaScript BigInt31. JavaScript Array Iterations32. Regular Expression Assertions33. JavaScript Array Const34. RegExp Quantifiers35. JavaScript RegExp Patterns36. JavaScript RegExp Objects37. RegExp Methods

On this page

JavaScript Math.random()JavaScript Random IntegersSummaryA Proper Random Function