Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 Groovy Script 中生成特定范围内的随机数?我想生成 10,000 到 90,000 之间的数字
以下是我到目前为止的尝试:
1.
Math.abs(new Random().nextInt() % 10000) +90000
2.
(int)(10000 + 90000*Math.random())
这两个都生成一个随机数是的,但它们不在10,00-90,000的范围内
我发现了我的错误:我只需要将 90000 和 10000 切换为 1。正确的代码:
Math.abs(new Random().nextInt() % 90000) +10000