以下是使用 JavaScript 编写的生成 20 到 150 之间随机数的函数: ```javascript function getRandomNumber() { return Math.floor(Math.random() * (150 - 20 + 1) + 20); } ``` 在上述代码中,`Math.random()` 会生成一个 0 到 1 之间的随机小数。通过乘以 `(150 - 20 + 1)` 得到 0 到 131 之间的随机小数,然后加上 20 得到 20 到 151 之间的随机小数,最后使用 `Math.floor()` 函数取整,得到 20 到 150 之间的随机整数。 [2024-08-22 21:08:26 | AI写代码神器 | 170点数解答]