Generating Random Numbers in JavaScript

It’s more than Math.random()

Image by NeONBRAND

The Math.random() will generate a pseudo-random floating number (a number with decimals) between 0 (inclusive) and 1 (exclusive).

Here, the random number doesn’t mean that you always get a unique number. It generates the same number only after some interval. The interval here is very long, so you probably won’t get the same number twice.

To generate a random number, we can use:

Math.random(); //0.6140915758927235 may be  you get different value

Defining our own random number function:

function rand(maxLimit = 100) {
   return Math.random() * maxLimit;
}   
rand(10); // 5.500949568251157

To get whole numbers, we can use:

  • Math.floor() → to round down
  • Math.ceil() → to round up
  • Math.round() → to nearest number
// using Math.floor
function rand(maxLimit = 100) {
 let rand = Math.random() * maxLimit;
 return Math.floor(rand);
}
// the above code generates any numbers between 0 to maxLimit-1

To get a random number between two numbers (min included, max excluded):

function rand(min, max) {
  let randomNum = Math.random() * (max - min) + min;
  return Math.floor(randomNum);
}
rand(10, 20);

In the above code, (max-min) + min is just to handle if there is any case when the max is less than the min. In such a case, where max is less than min:

max = 5, min = 10
(max - min) + min  => (5 - 10) + 5  => -5  + 5 => 0

To get random numbers including min and max element, we can change Math.floor to Math.round :

function rand(min, max) {
   let randomNum = Math.random() * (max - min) + min;
   return Math.round(randomNum);
}
rand(10, 20);

Generating Random Hex Colours

We know that hex colours have 1–9 and a-f .

To generate random colours:

  • Store hex values on array
  • Use math.rand to get index
  • Create a color string of length six or three.

https://gist.github.com/Jagathishrex/13a7dab8cff2e9e904b5948bd1eaa35b#file-random_color-js

Thanks for reading!

Leave a comment

Design a site like this with WordPress.com
Get started