如何在Java中生成随机正数和负数

2022-01-17 00:00:00 random numbers java

我正在尝试在原始数据类型 short 的范围(-32768、32767)上生成随机整数.java Random 对象只生成正数.我将如何在该间隔上随机创建数字?谢谢.

I am trying to generate random integers over the range (-32768, 32767) of the primitive data type short. The java Random object only generates positive numbers. How would I go about randomly creating numbers on that interval? Thanks.

推荐答案

你随机对 (0, 32767+32768) 然后减去 32768

You random on (0, 32767+32768) then subtract by 32768

相关文章