Ads by ProfitSence
Close

Generate random number in JavaScript

Last Updated on Wednesday 5th Oct 2022

random number generator

Generate a random number Math.random()

			
					console.log(Math.random())
// 0.4906656564949319

			
	

To generate a number that is a whole number rounded down between 1 and 10

			
					Math.floor(Math.random())
 // 0

			
	

number generator

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

// 66

			
	

The + 1 makes it so its not 0.

			
					
Math.floor(Math.random() * 10) + 1

// 8