Home » JavaScript

Generate random number within a range in JavaScript

JavaScript code to generate random numbers within a range - In this article, we will learn how we can generate a random number using Math.random() and some mathematical manipulation between a range?
Submitted by Abhishek Pathak, on October 02, 2017

JavaScript is a front-end scripting language. Many a times we need to generate random numbers between a range, say for an input. JavaScript has its own built in Math library and under that we have random function, which is like 'Math.random()', but it generates floating numbers between 0 and 1.

So, manipulating it with some other Math functions and simple maths logic, we can generate random numbers between a range. Here, is a simple program to do so.

JavaScript code to generate random numbers within a range

function randomNumber(start, end) {
  var diff = end - start;
  return Math.floor((Math.random)*diff + start);
}
console.log(randomNumber(1111, 9999));

Explanation

Looks unusual? It might look by just looking but it is not actually.

Only a simple logic is followed. First, we have taken the difference between starting range and end range.

As we know, 'Math.random()' returns numbers between 0 and 1. So the minimum number it will return is 0. Now if a number is multiplied with 0 (diff here), it will be 0. Adding the starting range to 0 will give the starting number itself.

Now, the maximum number randomly generated will be 1. If this is multiplied with difference, it will result difference. Now adding this difference to starting range, will give out nothing but the end range. Simple maths, right? Likewise, the maths goes for all the floating numbers between 0 to 1.

If you find this useful or a better way, leave down comments below.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.