Page 1 of 1

Wtf Java

Posted: Mon Jan 20, 2014 12:15 am
by Xaos
WTF Java. Your random number generation is stupid. Why can't you just be like PHP and have a rand(X,Y)? Why must you make me do:

(int)(x * Math.random());

WHY

/rant

Re: Wtf Java

Posted: Mon Jan 20, 2014 12:54 am
by hallsofvallhalla
try getting a random number in Cobol then get back to me



:P

Re: Wtf Java

Posted: Mon Jan 20, 2014 12:58 am
by Xaos
hallsofvallhalla wrote:try getting a random number in Cobol then get back to me



:P

You know, there was a time in life when I thought it'd be really cool to learn BASIC when I get into college and take some low-level stuff.

I'm really second guessing that now.

Re: Wtf Java

Posted: Mon Jan 20, 2014 3:02 pm
by Jackolantern
There are a number of languages like this, including Javascript. I am not sure where it originated (C random number generation is different), but it out-dates Java. I suppose it is to allow random floats as well as integers.

Re: Wtf Java

Posted: Mon Jan 20, 2014 6:20 pm
by Xaleph
The value between 0 and 1 has to do with uniform distribution probability. Any number between a given set A and B is equally as likely to come out on top. Given this context, most language-specific random generation use this approach. C showed the way, the rest followed and yes, some have made it easier to work with. However, PHP`s version is _not_ a RNG, neither is Java`s, or C`s for that matter.. This has to do with the way RNG is defined in the languages. All these RNG`s need a base seed for the left and right ( lower and outer ) bound to work with. Despite the fact that, yes, you can change the seed, if you or someone else finds the seed, they can, either by approximation or plain calculations, retrace your number.

This has to do with logic. Computers are dumb by default, the instructions given define how strong or weak a given algorithm is, since the more rules you apply, the more complex an algorithm becomes, so will the time to evaluate that given algorithm.

Anyway, in short, sure, the syntax is a bit different, but under the hood, it`s still exactly the same algorithm. In Java, you can use the Math.Random class and use something like

Random R = new Random();
int ri = R.nextInt();

to get integer values.