OiO.lk Blog java Random calculating anomalies in Java program
java

Random calculating anomalies in Java program


I have a java program game that I am making that is having some random calculation anomalies. The way that the program works, the player can spend money on stocks and then it 10 game turns the money is returned to the player with either an increase in stock value or a decrease (increase or decrease can vary anywhere from 25% to 399%).

sample code block has the following variables…

none, skipturn1, and start1 are Boolean values

calc1 and calc2 are double values

all other variables are int values

if ((playturn1 == 10) || (playturn1 == 20) || (playturn1 == 30) || (playturn1 == 40)) {
if ((skipturn1 == false) && (stock1 > 0) && (start11 == false)) {
start11 = true;
int stockresults1 = new Random().nextInt(74)+25;
int stockresults11 = new Random().nextInt(10)-6;
int stockresults111 = new Random().nextInt(10)+1;
if (stockresults11 < 0) {
stockresults11 = 0;
}

else {
none = false;
}

calc1 = stockresults1;
calc1 = calc1 / 100;
calc1 = calc1 + 1;
calc1 = calc1 + stockresults11;
calc2 = calc1 - 1;
calc2 = calc2 * 100;
calc3 = (int)calc2;
calc2 = stock1 * calc1;
calc1 = calc2 - stock1;
calc1 = stock1 - calc1;
calc1 = calc1 * 10;
calc1 = calc1 + 5;
calc1 = (int)calc1;
calc1 = calc1 / 10;
calc4 = (int)calc1;
calc2 = calc2 * 10;
calc2 = calc2 + 5;
calc2 = (int)calc2;
calc2 = calc2 / 10;
calc5 = (int)calc2;

if (calc4 < 0) {
calc4 = 0;
}

else {
none = false;
}

if (stockresults111 < 5) {
JOptionPane.showMessageDialog(null, "<html>Your stock investment has matured and is ready to withdraw.<br/><br/>The value of your investment has decreased by " + calc3 + "%. Your stock investment is currently worth $" + calc4 + ".<br/>You have deposited this amount into your account. Collect $" + calc4 + ".");
stock1 = 0;
money1 = money1 + calc4;
frame.dispose();
skipturn1 = true;

} else if (stockresults111 > 4) {
JOptionPane.showMessageDialog(null, "<html>Your stock investment has matured and is ready to withdraw.<br/><br/>The value of your investment has increased by " + calc3 + "%. Your stock investment is currently worth $" + calc5 + ".<br/>You have deposited this amount into your account. Collect $" + calc5 + ".");
stock1 = 0;
money1 = money1 + calc5;
frame.dispose();
skipturn1 = true;

} else {
none = false;
}
  }
    }

In testing, 95% of the time the code works flawlessly. Occasionally though, I get some random calculation anomalies where my programs calculation will be off by an int of up to 25. One example of this that occurred during testing occurred when I had my stock1 value at 1000. An increase in my stock value of 39% occurred which should have yielded the following results.

1000 * 1.39 = 1390

However my program reached the following result instead 1400

I have considered that this could be a rounding error due to me using a double value rather than a bigdecimal value, however, I don’t understand how such an error could yield results that are this far off (and only in very rare instances).

I also tested this theory out with a test program using the same values that caused this error to occur in my main program to see if I could duplicate the error.

import static java.lang.System.out;
public class test {
public static void main(String[] args) {
double a = 1.39;
int b = 1000;
a = b * a;
a = a * 10;
a = a + 5;
a = (int)a;
a = a / 10;
b = (int)a;
out.println(b);
}
  }

The test program, however, reached the correct result of 1390. I don’t understand why this random anomaly is occurring.



You need to sign in to view this answers

Exit mobile version