JavaScript获取一个从最小值到最大值的随机数:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> /** *获取一个区间[min, max)的随机整数 */ function getRandom(min, max) { return Math.floor(parseInt(Math.random() * (max - min))) + min; } console.log(getRandom(7, 9)); console.log(getRandom(-5, -3)); </script> </body> </html>