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.
我试图在原始数据类型short的范围(-32768,32767)上生成随机整数。 java Random对象仅生成正数。如何在该间隔内随机创建数字?谢谢。
8 个解决方案
#1
50
You random on (0, 32767+32768)
then subtract by 32768
你随机(0,32767 + 32768)然后减去32768
#2
22
Random random=new Random();
int randomNumber=(random.nextInt(65536)-32768);
#3
5
public static int generatRandomPositiveNegitiveValue(int max , int min) {
//Random rand = new Random();
int ii = -min + (int) (Math.random() * ((max - (-min)) + 1));
return ii;
}
#4
4
Generate numbers between 0 and 65535 then just subtract 32768
生成0到65535之间的数字,然后减去32768
#5
3
This is an old question I know but um....
这是一个我不知道的老问题,但是......
n=n-(n*2)
#6
1
([my double-compatible primitive type here])(Math.random() * [my max value here] * (Math.random() > 0.5 ? 1 : -1))
([我的双兼容原语类型])(Math.random()* [我的最大值] *(Math.random()> 0.5?1:-1))
example:
// need a random number between -500 and +500
long myRandomLong = (long)(Math.random() * 500 * (Math.random() > 0.5 ? 1 : -1));
#7
0
(Math.floor((Math.random() * 2)) > 0 ? 1 : -1) * Math.floor((Math.random() * 32767))
(Math.floor((Math.random()* 2))> 0?1:-1)* Math.floor((Math.random()* 32767))
#8
0
In case folks are interested in the double version (note this breaks down if passed MAX_VALUE or MIN_VALUE):
如果人们对双版本感兴趣(请注意,如果传递MAX_VALUE或MIN_VALUE则会出现故障):
private static final Random generator = new Random();
public static double random(double min, double max) {
return min + (generator.nextDouble() * (max - min));
}
#1
50
You random on (0, 32767+32768)
then subtract by 32768
你随机(0,32767 + 32768)然后减去32768
#2
22
Random random=new Random();
int randomNumber=(random.nextInt(65536)-32768);
#3
5
public static int generatRandomPositiveNegitiveValue(int max , int min) {
//Random rand = new Random();
int ii = -min + (int) (Math.random() * ((max - (-min)) + 1));
return ii;
}
#4
4
Generate numbers between 0 and 65535 then just subtract 32768
生成0到65535之间的数字,然后减去32768
#5
3
This is an old question I know but um....
这是一个我不知道的老问题,但是......
n=n-(n*2)
#6
1
([my double-compatible primitive type here])(Math.random() * [my max value here] * (Math.random() > 0.5 ? 1 : -1))
([我的双兼容原语类型])(Math.random()* [我的最大值] *(Math.random()> 0.5?1:-1))
example:
// need a random number between -500 and +500
long myRandomLong = (long)(Math.random() * 500 * (Math.random() > 0.5 ? 1 : -1));
#7
0
(Math.floor((Math.random() * 2)) > 0 ? 1 : -1) * Math.floor((Math.random() * 32767))
(Math.floor((Math.random()* 2))> 0?1:-1)* Math.floor((Math.random()* 32767))
#8
0
In case folks are interested in the double version (note this breaks down if passed MAX_VALUE or MIN_VALUE):
如果人们对双版本感兴趣(请注意,如果传递MAX_VALUE或MIN_VALUE则会出现故障):
private static final Random generator = new Random();
public static double random(double min, double max) {
return min + (generator.nextDouble() * (max - min));
}