Generate random numbers in JavaScript by Cules Coding

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.

const getRandomNum = () => {
reutrn Math.random()
}
console.log(getRandomNum())
console.log(getRandomNum())
console.log(getRandomNum())
console.log(getRandomNum())
// output
// 0.4656524910632813
// 0.36946529666025185
// 0.93309610877532
// 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

const random = Math.random
const floor = Math.floor
const getRandomNum = () => {
return floor(random() * 10)
}
setInterval(() => {
const num = getRandomNum()
console.log(num)
}, 500)
// Output:
// 2
// 5
// 1
// 9
// 1
// 7
// 6
// 0
// 7
// 3
// 4
// 8
// 2
// 2
// 4
// 5
// 5
// 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

8.343487294263 --> 8
3.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

const random = Math.random
const floor = Math.floor
const getRandomNumInRange = (min, max) => {
return floor(random() * (max - min + 1)) + min
}
setInterval(() => {
const num = getRandomNumInRange(10, 20)
console.log(num)
}, 500)
// output:
// 11
// 13
// 14
// 20
// 13
// 13
// 10
// 16
// 17
// 10
// 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

difference = 10
random numbers = [
2,3,4,6,10 ...
]

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.

const getRandomInt = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min

That's how we get a random number in a range.

Shameless Plug

I have made few project based videos with vanilla HTML, CSS, and JavaScript.

You will learn about:

  • Javascript intersection observer to add cool effects
  • DOM manipulation
  • Aligning elements with CSS positions.
  • How to make responsive websites.
  • How to create slide based webpage.

These will be great projects to brush up on your front end skills.

If you are interested you can check the videos.

You can also demo the application from here:

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project.

About me

Why do I do what I do?

The Internet has revolutionized our life. I want to make the internet more beautiful and useful.

What do I do?

I ended up being a full-stack software engineer.

What can I do?

I can develop complex full-stack web applications like social media applications or e-commerce sites. See more of my work from here

What have I done?

I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.

Screenshot

Homepage

More screenshots

I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.

Want to work with me?

I am looking for a team where I can show my ambition and passion and produce great value for them. Contact me through my email or any social media as @thatanjan. I would be happy to have a touch with you.

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous PostBuild a random password generator app with vanilla JavaScript
Next PostEverything you need to know about Javascript Destructuring