Includes 0.0, Excludes 1.0
Math.random() 0.5653749472719645 Math.random() 0.18129516667898726 Math.random() 0.38332031525383603 Math.random() 0.11096588528106843 Math.random()
Includes integer 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
Math.floor(Math.random() * 10) 7 Math.floor(Math.random() * 10) 6 Math.floor(Math.random() * 10) 9 Math.floor(Math.random() * 10) 0 Math.floor(Math.random() * 10) 7
Includes integer 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.
Math.floor((Math.random() * 10) + 1) 10 Math.floor((Math.random() * 10) + 1) 5 Math.floor((Math.random() * 10) + 1) 6 Math.floor((Math.random() * 10) + 1) 4 Math.floor((Math.random() * 10) + 1) 10 Math.floor((Math.random() * 10) + 1) 1
Generates the two integers 0 and 1.
Math.floor((Math.random() * 2)) 1 Math.floor((Math.random() * 2)) 1 Math.floor((Math.random() * 2)) 0 Math.floor((Math.random() * 2)) 0 Math.floor((Math.random() * 2)) 0 Math.floor((Math.random() * 2)) 1 Math.floor((Math.random() * 2)) 0
Use the double equals operator to get the truthy value from 0 and 1:
Math.floor((Math.random() * 2)) == true false Math.floor((Math.random() * 2)) == true false Math.floor((Math.random() * 2)) == true true Math.floor((Math.random() * 2)) == true true Math.floor((Math.random() * 2)) == true false
Get a random integer from (including) a minimum integer to (including) a max integer.
This code generates integers in range: 2, 3, 4, 5, 6 and 7
const min = 2; const max = 7; Math.floor((Math.random() * (max - min + 1)) + min) 4 Math.floor((Math.random() * (max - min + 1)) + min) 2 Math.floor((Math.random() * (max - min + 1)) + min) 6 Math.floor((Math.random() * (max - min + 1)) + min) 7 Math.floor((Math.random() * (max - min + 1)) + min) 3 Math.floor((Math.random() * (max - min + 1)) + min) 4