# Random number generator javascript, with it within a range and meet special needs

Lionsure 2020-06-20 Original by the website

There is also a built-in function for generating random numbers in j4avascript, namely the random number generating function random(); in addition, you can borrow the Date to generate a random number. In addition to the above two methods of generating random numbers, you can also use them in combination with arrays to generate special random numbers in a specified range to meet special needs.

### I. Random number generator javascript with the built-in function

The Math.random() method in javascritp generates a floating-point number(decimal) between 0 and 1, unlike the Random() method in C#, which can arbitrarily specify the range of generated random numbers. So after using random() to generate floating point numbers, you need to multiply a corresponding value and also use other methods of Math to help generate random numbers.

1. Use Math.floor(Math.random()) to generate random numbers

Math.floor(Math.random() * 100); //Generate random numbers from 0 to 99

Math.floor(Math.random() * 100 + 1); //Generate random numbers from 1 to 100

2. Use Math.ceil(Math.random()) to generate random numbers

Math.ceil(Math.random() * 100); //Generate random numbers from 1 to 100

Related instructions:

Math.floor() method is to take the floor(i.e. round down), such as Math.floor(6.6) = 6;

Math.ceil() method is to take the ceiling (i.e. round up), such as Math.ceil(6.6) = 7.

3. Use parseInt(Math.random()) to generate random numbers

parseInt(Math.random() * 100); //Generate random numbers from 0 to 99

parseInt(Math.random() * 100 + 1); //Generate random numbers from 1 to 100

4. Generate random number in javascript within a range

Since javascript does not provide the method that directly generates random number within a range, so you have to implement it yourself as follows:

Math.floor(Math.random() * (upper limit - lower limit + 1) + lower limit);

Math.ceil(Math.random() * (upper limit - lower limit) + lower limit);

ParseInt(Math.random() * (upper limit - lower limit + 1) + lower limit);

Choose one of the methods to encapsulate the function as follows:

//under: upper limit; over: lower limit
function GeneratRandom(under, over) {
switch (arguments.length) {
case 1: return Math.ceil(Math.random() * under);
case 2: return Math.ceil(Math.random() * (over - under) + under);
default: return 0;
}
}

Call:

GeneratRandom(100);//Generate random numbers from 1 to 100

GeneratRandom(100, 200);//Generate random numbers from 100 to 200

### II. Borrow Date Generate random number

var d = new Date();

var n = d.getSeconds(); //Generate random numbers from 0 to 59

var n1 = d.getMilliseconds(); //Generate random numbers from 0 to 999

var n2 = d.getMilliseconds() % 201; //Generate random numbers from 0 to 200

In addition to taking the seconds and microseconds of Date, you can also take hours and minutes(same as the random number generated by taking seconds).

### III. Generate random numbers to meet special needs

Sometimes, it is stipulated that the generated random number can only take some arbitrarily specified values; at this time, the above method cannot be used to meet the requirements. It is necessary to save the arbitrarily specified values in the array, and then get the values in the array with the generated random number(array subscript), the implementation code is as follows:

function GeneratRandom(arr)
{
var n = Math.ceil(Math.random() * (arr.length - 1));
return arr[n];
}

Call:

var arr = [ "18", "23", "26", "32", "46", "56" ];

GeneratRandom(arr);