I am trying to make a Java implementation of the Park-Miller-Carta PRNG random number generator.
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).
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: