Wtf Java

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Wtf Java

Post 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
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Wtf Java

Post by hallsofvallhalla »

try getting a random number in Cobol then get back to me



:P
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Wtf Java

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Wtf Java

Post 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.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Wtf Java

Post 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.
Post Reply

Return to “Coding”