Re: Please review this code
On Aug 23, 2:40 am, Arne Vajh=F8j <a...@vajhoej.dk> wrote:
Joshua Cranmer wrote:
guptanwrote:
public int getRandom(int max) {
return (max == 0) ? 0 : (Math.abs(random.nextInt()=
% max));
}
That code is very, very wrong for getting a uniform random number in th=
e
range [0, max]. Which is why it's generally not a good idea to reinvent
something already part of the core Java API. If you need to know why,
find 2^31 % 3.
And, FWIW, you'll also run into the problem with lower-bit short cycles
that arise out of linear congruential pseudorandom number generators.
It is unnecessary bad, because the "badness" is easy avoidable.
How bad it is will depend on how big max is and whether max is
a power of 2.
Arne
Thank you all,
Originally this code is targeted for CLDC1.0. so i couldn't use
floating point numbers.
I noted a behavior that when make random instance as static to use RNG
as a singleton random property of the class becomes predictable.
guptan