ZAIN
0
Q:

get random number within range javascript

const randomFloat = (min, max) => Math.random() * (max - min) + min;
4
function getRandomNumberBetween(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

//usage example: getRandomNumberBetween(20,400); 
25
const randomInteger = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
3
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}
4
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
1

Math.floor(Math.random() * 10);     // returns a random 
  integer from 0 to 9
 
0
<script>  
  
// Function to generate random number  
function randomNumber(min, max) {  
    return Math.random() * (max - min) + min; 
}  
  
document.write("Random Number between 1 and 5: ")  
  
// Function call 
document.write( randomNumber(1, 5) );  
</script>                                   
0
let object = random(0, 50);
// making "object" a random number between 0 and 50.

console.log(object);
// printing object in the console
0

New to Communities?

Join the community