Random

JavaScript Randomization examples


Random number in a range

Generate a random number in a range. Source

  • Example:

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

Random number with fixed length

Generate a random number with fixed length. Source

  • Example:

    function getRandumNumber(length) {
        const min = Math.pow(10, (length-1));
        const max = Math.pow(10, (length));
        return Math.floor(Math.random() * (max - min) + min);
    }

Random string

Generate a random string. Source

  • Example:


Random UUID

Generate a random UUID. Source

  • Example:


Random color

Generate a random color. Source

  • Example:


Random date

Generate a random date. Source

  • Example:


Random date in a range

Generate a random date in a range. Source

  • Example:


Last updated

Was this helpful?