SoFunction
Updated on 2025-03-10

Summary of various methods of generating random numbers by JS (random numbers of different ranges and types)

1, generate random numbers in the range [ 0, 1 ) (greater than or equal to 0, less than 1)

(1) Use the random() method to return a pseudo-random number between 0 and 1 (including 0, excluding 1).

()

(2) Here is a test sample

var random = ();
(random);

2. Generate random numbers in the range [ n, m ) (greater than or equal to n, less than m)

(1) This is the simplest because it is consistent with the characteristics of random. Just use the following formula:

()*(m-n)+n

(2) For example, the random floating point number in the range [10,15) is generated below.

var random1 = ()*(15-10)+10;
var random2 = ()*(15-10)+10;
var random3 = ()*(15-10)+10;
(random1);
(random2);
(random3);

3. Generate random numbers in the range [n,m], (n,m), (n,m]

Because of the characteristics of random, it is a little more troublesome to obtain floating point numbers in these intervals, and some judgment is required to meet the requirements.

//Get the random number of the range [n,m]function fullClose(n,m) {
   var result = ()*(m+1-n)+n;
   while(result>m) {
       result = ()*(m+1-n)+n;
   }
   return result;
}
 
//Get random number in range (n,m)function fullOpen(n,m) {
   var result = ()*(m-n)+n;
   while(result == n) {
       result = ()*(m-n)+n;
   }
   return result;
}
 
//Get the random number of (n,m] rangefunction leftOpen(n,m) {
   var result = ()*(m-n+1)+n-1;
   while(result<n) {
       result = ()*(m-n+1)+n-1;
   }
   return result;
}

Generation of random integers

To generate random integers, we also need to use the following two methods: (num): Round num (num): Round num downward, that is, return the integer part of num. Of course, we can also use the parseInt() method instead.

1, randomly generate two integers: 0 and 1

(1) The following method can randomly obtain 0 or 1, and the chances of obtaining them are relatively balanced.

(())

(2) Here is a test sample

var random1 = (());
var random2 = (());
var random3 = (());
(random1);
(random2);
(random3);

2, generate random integers in the range [ 0, n ) (greater than or equal to 0, less than n)

(1) The following method generates a random integer from 0 to n-1 (the chances of obtaining these n numbers are balanced)

(()*n)

(2) For example, the following generates several random integers of 0 to 4 (including 0 and 4).

var random1 = (()*5);
var random2 = (()*5);
var random3 = (()*5);
(random1);
(random2);
(random3);

3. Generate random integers in the range [1, n] (greater than or equal to 1, less than or equal to n)

(1) The following method generates a random integer from 1 to n (the chances of obtaining these n numbers are balanced)

(()*n)+1

(2) For example, the following generates several random integers of 1 to 5 (including 1 and 5).

var random1 = (()*5)+1;
var random2 = (()*5)+1;
var random3 = (()*5)+1;
(random1);
(random2);
(random3);

4. Generate random integers in the range [min, max] (greater than or equal to min, less than or equal to max)

(1) The following method generates a random integer with a minimum value of min and a maximum value of max.

(()*(max-min+1))+min

(2) For example, the following generates several random integers of 5 to 10

var random1 = (()*5)+1;
var random2 = (()*5)+1;
var random3 = (()*5)+1;
(random1);
(random2);
(random3);

Generation of random strings

1. Generate a pure numeric string with a specified number of digits.

//Generate n-digit stringfunction randomNum(n){
  var res = "";
  for(var i=0;i<n;i++){
    res += (()*10);
  }
  return res;
}
 
//test(randomNum(3))
(randomNum(5))
(randomNum(7))

2. Generate a string of strings with numeric letters with specified number

//Generate n-digit alphabetical stringsfunction generateMixed(n) {
  var chars = ['0','1','2','3','4','5','6','7','8','9',
              'A','B','C','D','E','F','G','H','I','J','K','L','M',
              'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  var res = "";
  for(var i = 0; i < n ; i++) {
     var id = (()*36);
     res += chars[id];
  }
  return res;
}
 
//test(generateMixed(3))
(generateMixed(5))
(generateMixed(7))

Compare the supplementary version of the above code and add numbers, upper and lower case characters

function getjl(n){
var chas=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var binint=["0","1","2","3","4","5","6","7","8","9"];
var res="";
for(var i=0; i < n; i++){
		var id = (() * 62);
		res+=chas[id];
	}
						
return res;
}
//test(getjl(3))
(getjl(5))
(getjl(7))

Summarize

This is the end of this article about the summary of various methods of JS generating random numbers. For more related JS generating random numbers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!