Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 29, 2022 12:46 pm GMT

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.randomconst floor = Math.floorconst 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 --> 83.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.

By the way, this blog is originally published on cules coding website.. I would be glad if you give it a visit.

Generate random numbers in JavaScript by Cules Coding

Get a random integer in a range

Let's see how we can get a random number between 10 and 20

const random = Math.randomconst floor = Math.floorconst 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 = 10random 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.

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

If you are interested you can check the videos.

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.

See my work from here

Contacts

Videos might you might want to watch:





Blogs you might want to read:


Original Link: https://dev.to/thatanjan/generate-random-numbers-in-javascript-2gh6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To