Home > Web Front-end > JS Tutorial > body text

What are the ways to generate random numbers in js

DDD
Release: 2023-09-04 11:28:00
Original
2294 people have browsed it

The methods for generating random numbers in js are: 1. Use the random function to generate random numbers between 0-1; 2. Use the random function and a specific range to generate random integers; 3. Use the random function and the round function Generate random integers between 0-99; 4. Use the random function and other functions to generate more complex random numbers; 5. Use the random function and other functions to generate random decimals within a range; 6. Use the random function and other functions to generate a range Random integers or decimals within.

What are the ways to generate random numbers in js

#In JavaScript, there are many ways to generate random numbers. The following are some commonly used methods:

Use Math.random() to generate a random number between 0-1:

var random = Math.random();  
console.log(random);  // 输出一个0到1之间的随机数,包括0但不包括1
Copy after login

Use Math.random( ) and a specific range to generate a random integer:

var min = 1;   
var max = 100;  
var random = Math.floor(Math.random() * (max - min + 1)) + min;  
console.log(random);  // 输出1到100之间的随机整数
Copy after login

Here, Math.random() generates a random number between 0 and 1, and then we multiply this random number by max - min 1, Get a random number within the desired range (including min and max). We then use Math.floor() to round this random number down to get an integer. Finally, we add min to this integer to get the final random integer.

Use Math.random() and Math.round() to generate a random integer between 0-99:

var random = Math.round(Math.random() * 99);  
console.log(random);  // 输出0到99之间的随机整数
Copy after login

Use Math.random() and other functions to generate more complex random numbers:

function getRandomInt(min, max) {  
  min = Math.ceil(min);  
  max = Math.floor(max);  
  return Math.floor(Math.random() * (max - min + 1)) + min;   
}  
console.log(getRandomInt(10, 50));  // 输出大于等于10且小于等于50的随机整数
Copy after login

This function first ensures that min and max are integers, then generates a random number within the range (including min and max), and finally returns this random number integer.

Use Math.random() and other functions to generate random decimals within a range:

function getRandomFloat(min, max) {  
  return Math.random() * (max - min) + min;   
}  
console.log(getRandomFloat(10, 50));  // 输出在10到50之间的随机小数
Copy after login

Use Math.random() and other functions to generate a range Random integers or decimals:

function getRandomIntOrFloat(min, max) {  
  if (min === Math.floor(min) && max === Math.floor(max)) {  
    return Math.floor(Math.random() * (max - min + 1)) + min;   
  } else {  
    return Math.random() * (max - min) + min;   
  }  
}  
console.log(getRandomIntOrFloat(10, 50));  // 可以输出大于等于10且小于等于50的随机整数或小数
Copy after login

This function first checks whether min and max are integers. If so, returns a random integer within the range (including min and max). If not, a random decimal number within the range (including min and max) is returned.

The above are some methods of generating random numbers in JavaScript. You can choose the appropriate method according to your needs.

The above is the detailed content of What are the ways to generate random numbers in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!