Jump to content

Whats up with this?

BamesJond

So I wanted to test if the RNG in java is legit and came up with this:

 

 

 

fad3f37af5d4f8155a23d3110635c3da.png

 

 

 

Which basically shows the percentage of how many of each of 3 numbers were generated.

But if I add one more 0 to the counter, this happens:

 

 

 

0cbeeb3d6c520451d8aa18fd9b3a3a9a.png

 

 

Can anyone explain WHY this happens.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Are you sure that int in java can be that big?

Link to comment
Share on other sites

Link to post
Share on other sites

Are you sure that int in java can be that big?

Well if I add one more 0 to the counter, it gives me an out of range error.

Link to comment
Share on other sites

Link to post
Share on other sites

Well if I add one more 0 to the counter, it gives me an out of range error.

Try and long instead of an int and see what that does.

|| Processor: AMD Ryzen 5 2600 || RAM: 32GB (4x8GB) Corsair DDR4 Vengance (3000) || Motherboard: ASUS Prime B450-Plus || Graphics Card: Gigabyte RTX2070 || Storage: 750GB SSD (2 Drives), 3TB HDD (2 Drives) || Case: NZXT H500 || Power Supply: be quiet! Pure Power 11 600W || 

 

Link to comment
Share on other sites

Link to post
Share on other sites

actually an int should hold 10 digits so it might not be that.

|| Processor: AMD Ryzen 5 2600 || RAM: 32GB (4x8GB) Corsair DDR4 Vengance (3000) || Motherboard: ASUS Prime B450-Plus || Graphics Card: Gigabyte RTX2070 || Storage: 750GB SSD (2 Drives), 3TB HDD (2 Drives) || Case: NZXT H500 || Power Supply: be quiet! Pure Power 11 600W || 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think the numbers are to big for a double. Try a long double assuming there is such thing in Java (there might not be)

|| Processor: AMD Ryzen 5 2600 || RAM: 32GB (4x8GB) Corsair DDR4 Vengance (3000) || Motherboard: ASUS Prime B450-Plus || Graphics Card: Gigabyte RTX2070 || Storage: 750GB SSD (2 Drives), 3TB HDD (2 Drives) || Case: NZXT H500 || Power Supply: be quiet! Pure Power 11 600W || 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think that AleksaNS is right. I believe that when you initialize your 'a' value Java treats it as:

double a = (double)(yks + kaks + kolm);

Meaning that the summation is done as integers.

Looping 100,000,000 times is fine, but taking it up to 1,000,000,000 takes the sum over the highest limit of what Java 32 bit signed integers can store (which is 2,147,483,647) and you end with an overflow, which causes the a value to be negative.

I haven't copied your code to test this, but try casting your 'yks', 'kaks' and 'kolm' values to doubles or longs as suggested by mopman, before doing your summation.

 

quick tip edit: To get the highest value of an int, double or long, just use Integer.MAX_VALUE, Double.MAX_VALUE or Long.MAX_VALUE 

Link to comment
Share on other sites

Link to post
Share on other sites

It also looks like, from reading the API, you'll end up getting a sequence of random numbers from doing one new Random(); and the sequence will loop. It's interesting that the Java implementation operates with 48-bit values but only returns 32 most-significant bits. Here's more info if you want to read about the algorithm used to create the pseudo-random numbers.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×