请帮我把我的随机数生成Java中工作 [英] Please help me get my random number generating working in Java
问题描述
我试图做一个Java实现的公园 - 米勒 - 宪章PRNG随机数生成器(可能更快?)
下面是在ActionScript 3的从这里。
收益率(_currentSeed =(_currentSeed * 16807)%2147483647)/为0x7FFFFFFF
+ 0.000000000233;
我没有多少运气得到这个在Java中工作:
INT种子= 20; //例如。
公共双随机(){
种子=(籽* 16807)%2147483647;
返回种子/ +为0x7FFFFFFF 0.000000000233;
}
这总是返回 2.33E-10
。任何想法,我做错了在Java中? (AS3的code返回 0.0001565276181885122
,然后 0.6307557630963248
与的种子前两个回应 20
)。
种子/为0x7FFFFFFF
是一个整数操作,因为这两个参数都是整数。整数除法总是向下舍入真的结果。在这种情况下,真正的结果为0和1之间,所以操作总是返回0
要得到一个浮点结果,参数至少有一个必须是一个浮点数,它可以实现这样的:
收益率(双)种子/ +为0x7FFFFFFF 0.000000000233;
I am trying to make a Java implementation of the Park-Miller-Carta PRNG random number generator (maybe faster?)
Below is the implementation of the Random function in ActionScript 3 from here.
return (_currentSeed = (_currentSeed * 16807) % 2147483647) / 0x7FFFFFFF
+ 0.000000000233;
I am not having much luck getting this to work in Java:
int seed = 20; //for example.
public double random() {
seed = (seed * 16807) % 2147483647;
return seed / 0x7FFFFFFF + 0.000000000233;
}
This always returns 2.33E-10
. Any ideas what I am doing wrong in Java? (the AS3 code returns 0.0001565276181885122
, then 0.6307557630963248
for the first two responses with a seed of 20
).
seed / 0x7FFFFFFF
is an integer operation, since both arguments are integers. Integer division always rounds the "true" result downwards. In this case, the true result is between 0 and 1, so the operation always returns 0.
To get a floating-point result, at least one of the arguments must be a float, which can be achieved like this:
return (double)seed / 0x7FFFFFFF + 0.000000000233;
这篇关于请帮我把我的随机数生成Java中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!