Generate random numbers in JavaScript
In this blog, you will learn how to generate random numbers in javascript.
There is no straightforward way to generate a random number. You have to use a little trick.
Math.random
function gives us a random number between 0 and 1.
1const getRandomNum = () => {2 reutrn Math.random()3}45console.log(getRandomNum())6console.log(getRandomNum())7console.log(getRandomNum())8console.log(getRandomNum())910// output11// 0.465652491063281312// 0.3694652966602518513// 0.9330961087753214// 0.6869999317373736
That's not what you want. You need an integer. To get an integer you need to multiply the random number with another number. And then you have to floor the value.
Get a random integer
1const random = Math.random2const floor = Math.floor34const getRandomNum = () => {5 return floor(random() * 10)6}78setInterval(() => {9 const num = getRandomNum()1011 console.log(num)12}, 500)1314// Output:15// 216// 517// 118// 919// 120// 721// 622// 023// 724// 325// 426// 827// 228// 229// 430// 531// 532// 0
By multiplying a number you have set an upper limit for the random number. We have floored the value because we don't want a floating-point number. Math.floor function gives us this
18.343487294263 --> 823.37294263 --> 3
Notice that we never get 10 as our random number. Because that is our limit. To include 10, just add 1
to the number.
Get a random integer in a range
Let's see how we can get a random number between 10 and 20
1const random = Math.random2const floor = Math.floor34const getRandomNumInRange = (min, max) => {5 return floor(random() * (max - min + 1)) + min6}78setInterval(() => {9 const num = getRandomNumInRange(10, 20)1011 console.log(num)12}, 500)1314// output:15// 1116// 1317// 1418// 2019// 1320// 1321// 1022// 1623// 1724// 1025// 16
Explanation: Now we need something between two numbers. So, we generate random numbers up to the difference of our range. In our case, it will be
1difference = 102random numbers = [3 2,3,4,6,10 ...4]
Then we add our minimum number with a random number. And that number definitely will be in the range.
You can write the code in one line.
1const getRandomInt = (min, max) =>2 Math.floor(Math.random() * (max - min + 1)) + min
That's how we get a random number in a range.
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: