Generate Random Whole Numbers using JavaScript

Aditya SharmaAditya Sharma
1 min read

Hey there, today I was thinking what could be my lucky number and unfortunately I was unable to figure it out on myself, so I thought why not take help of our favorite programming language JavaScript to know about that :

To generate whole numbers, we combine the function of Math.random() with Math.floor(). This means we generate a random number, then round it down to its nearest whole number.

Example:

To generate a whole number between 0 and 24:

  • We use the Math.random() function to generate a random decimal.

  • We multiply the decimal by 25.

  • We use the Math.floor() function to round down the number to its nearest whole number.

We can see this in the code below, which will print numbers between 0 and 24 to the console:

var randomWholeNumber = console.log(Math.floor(Math.random() * 25));
//Output :  16

Well it's this easy if you find it useful, leave a like and comment it won't cost you any money, and for most important go run this piece of code in your IDE.

11
Subscribe to my newsletter

Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Sharma
Aditya Sharma

I am a web developer from Bengaluru, India. My mission is to turn your vision into a compelling digital experience. I'm here to collaborate and bring your ideas to life. Let's build something amazing together!