What are the need of Random object in JS?

0 votes
21 views
Ravi Vishwakarma asked 13 days ago in Web Application by Ravi Vishwakarma

What are the need of Random object in JS?

1 Answer

0 votes
Ethan Karla answered 11 days ago by Ethan Karla
The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
Syntax:
 Math.random();
Example:
<script>

function getRandomInt(max,min) {
  var rand=Math.random();
  console.log('rand: '+rand);
  min = Math.ceil(min);
  console.log('min: '+min);
  max = Math.floor(max);
  console.log('max: '+max);
  var res=rand * (max - min) + min;
  console.log('res :'+res);
  return Math.floor(res);
}
console.log(getRandomInt(10.30,4.20));
</script>