I need a method that generates a positive whole number, any number.
Couldn’t found any answer that specify those exact requirements – any positive && whole number.
Basically I’m looking for something like that:
const randomNumber = getWholePositiveNum();
2
Answers
Here is a solution I found:
Math.random()
provides an evenly distributed decimal between 0 and 1Number.MAX_SAFE_INTEGER
scales it to the desired range - max safe integer value in JS, which is 9007199254740991Math.floor()
converts it to an integer (whole)The subtle crypto interface provides a method. Node.js includes this library as well. From MDN