javascriptrandom

How to generate a random whole positive number in Javascript with no specific range


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();

Solution

  • Here is a solution I found:

    console.log(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + 1)