- Views: 11
- Report Article
- Articles
- Technology & Science
- Electronics
Using JavaScript to Create a Simple Random Number Generator
Posted: Jul 06, 2019
In this article i'll go over how to create a random number generator using some javascript. This guide will be aimed towards beginners, but it's also useful for anyone who wants to brush up on their knowledge of JS. Now, just to answer the question of "why is this useful" before it's asked, well, the basics of this can be used when you need any random "coin-flip" style action/reaction in your projects. It's also useful for dynamic animations but that's a bit too advanced for this guide!
So let's see some code:
Math.floor(Math.random() * (1 + High - Low)) + Low
So this will be primarily what we work with. Math.floor ensures that there aren't any decimals, we only want to work with whole numbers here.
Math.random generates a random value between 0-1 aka 0.12236, 0.65 etcThe "1" is optional and keeps it from ever landing on 0.Replace "high" and "low" with your own range
This can be elaborated on to make something like:// Random number between 0 and 100 (inclusive)var bar = Math.floor(Math.random() * 101);console.log(bar);
If you want to get fancy with it you can create your on functions to keep everything nice and neat. So in doing so we could get something like this:function getRandomNumber(low, high) { var r = Math.floor(Math.random() * (high - low + 1)) + low; return r;}So once you have a function, you'll need call it like so:// Random number between 0 and 10 var genone = getRandomNumber(0, 10);console.log(genone); // Random number between 0 and 100 var gentwo = getRandomNumber(0, 100);console.log(gentwo); // Random number between 5 and 25 var genthree = getRandomNumber(5, 25);console.log(genthree);
Now this is all fine and dandy, but it can appear a bit sloppy so let's look at some more ways to go about generating random numbers between a set range. Now bear with me, this is going to be a bit strange and may seem redundant, but it's worth knowing. Back to the roots:Math.floor(Math.random() * (1 + High - Low)) + Low
So let's look at that value of 1 that we're always adding and talk about why we add it. The thing with math.floor is that using that you will never be able to hit the maximum integer within your range because of how it rounds down to the nearest integer. So if your maximum range was set to 1, you will only ever get something like.99999.
So to see this in an easier example let's look at:Math.floor(Math.random() * 40) + 10
Our lowest number we want to get is 10 and the highest is 50, aka our range.This will never return a 40 because math.random cannot return a 1, at most it will give us something like.99995 and that multiplied by 40 which gives us 39.99... rounded down thanks to math.floor to a 39. This means to fix this we'll have to do this: Math.floor(Math.random() * 41) + 10
Know that in this above example you could also do + 1 as well to get the same result.
This can be further added to in order to create random generators that add text, hypens and more to your strings. This is useful when creating Gift Card Code Generators Online.
Coding enthusiast and article writer, learning is my passion. Hope you enjoyed reading.