## Random number generator for dummies, myself very much included

ProgrammingJul 2021

I have often reverted to Googled to get a quick random number generating function; without investing a moment to understand its simple mechanic and saving myself future searches. Being a little dyslexic, I'd get all confused with the max - min + 1 + min portion of the function. Well, today is the day I untangle this mess of mins and maxes.

In essence, the random function in Javascript’s Math object returns a quasi-random floating point number between zero and one.

If I were looking for a number between 0 and 9, I could simply shift that comma by 1 decimal place by multiplying it by 10.

To make the 10 inclusive, I could increase it by 1; or simply multiply it by 10 + 1. This would increase the range of possible random numbers from 0-9 to 0-10.

What this means, is that I'm multiplying the result of the random function by the range of possible numbers I'm looking for, and adding one so as to make it inclusive.

To get a random number between 0 and 75, I can:

What if I want a number between a minimum and a maximum, say between 25 and 150? There are two parts to the process. First, I need to determine the range of numbers I want my number to be in between of — that is the range of numbers between 25 and 150. That can be achieved by subtracting 25 from 150. I'm therefore looking for one random number out of 75 possible numbers.

Then, I want my possible random number to be in between 25 and 150. To get one of 75 random numbers that start at least at 25, all I have to do is add 25 to my random number. 🤯

In essence, this is a random number multiplied by a range of numbers and bumped up by the starting point number.

Finally, the result can be rounded down to the lowest nearest integer; and that’s how you get a damn random number between 25 and 150. See function in Javascript below:

No items found.