0

I cant figure out why x=x-z gives me the values it does. x is supposed to decrease its value by z every 6th loop, for 1000 loops, making it less probable that mrnd (math random) is greater than poa (probability of acceptance).

int i = 0; 
double y = 0.9;
double x = 1.0;
double z = 0.1;

for (int o = 0; o < 1000; o++) {
    for (i = 0; i < 6; i++) {


        Double random = Math.random();
        Double mrnd = Math.floor(random*100)/100;

        double poa = (x*y);
        System.out.println("randomkalk " + mrnd);
        System.out.println("probability" + poa);

        if (poa < mrnd ) {
            System.out.println("accept change");
        };
        if (poa > mrnd )  {
            System.out.println("deny change");
        }

    }   
    System.out.println(x-z); // This gives the right output if x=x-z is not present
    System.out.println(" nr 6 \n \n ");
    x = x-z; // Gives wrong output 
}
Lore
  • 1,286
  • 1
  • 22
  • 57
TTT
  • 13
  • 2
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Fred Larson Sep 20 '19 at 13:47
  • 2
    You aren't doing something "every 6th iteration". Instead, for each iteration from a 1000, you do 6 additional iterations. – M. Prokhorov Sep 20 '19 at 13:50
  • Also, do you intend to have it so `x` is less than zero for most of your loop? Even if it decreased every 6th iteration from a 1000, that's means it'll be zero after 36th one, and from that point on your `poa` will always be less than `mrnd`. – M. Prokhorov Sep 20 '19 at 13:55
  • "Gives wrong output" - you should tell us what you'd expect and what you get instead. Note that for `x = 1.0` and `z = 0.1` `x = x - z ` (or in short `x -= z`) would result in a negative `x` pretty quickly. Just a guess here: could it be that you actually mean to just use `x *= y` in the outer loop (thus no need for `poa`)? That would decrease `x` by 10% each time but `x` would never become negative. – Thomas Sep 20 '19 at 13:56
  • Btw, why are you using `Double` here? `Math.random()` and `Math.floor()` return `double` so there's no need for all that (un-)boxing. – Thomas Sep 20 '19 at 13:58
  • Added a println with o right before 6, and it does run 1000 times only, and every 6th it prints " nr 6".. – TTT Sep 20 '19 at 14:01
  • The println at bottom is just for testing purposes now. What im trying to do is decrease x by z (0.1) every 6th iteration. – TTT Sep 20 '19 at 14:04

1 Answers1

0

As M. Prokhorov points out, you're not doing anything every 6th iteration. You're doing something 6 times every iteration. Making another loop inside your loop will only add more iterations of something; instead, try checking the counter on the outside loop.

int i = 0; 
double y = 0.9;
double x = 1.0;
double z = 0.1;

for (int o = 0; o < 1000; o++) {
    Double random = Math.random();
    Double mrnd = Math.floor(random*100)/100;

    double poa = (x*y);
    System.out.println("randomkalk " + mrnd);
    System.out.println("probability" + poa);

    if (poa < mrnd ) {
        System.out.println("accept change");
    };
    if (poa > mrnd )  {
        System.out.println("deny change");
    }

    if (o % 6 == 5) {
        x = x-z;
    }
}

Here, I use the modulus operator to check if we are on the 6th iteration

  • 0 % 6 == 0
  • 1 % 6 == 1
  • 2 % 6 == 2
  • 3 % 6 == 3
  • 4 % 6 == 4
  • 5 % 6 == 5 decrement
  • 6 % 6 == 0
  • 7 % 6 == 1
  • 8 % 6 == 2
  • 9 % 6 == 3
  • 10 % 6 == 4
  • 11 % 6 == 5 decrement
  • 12 % 6 == 0
mattbornski
  • 11,895
  • 4
  • 31
  • 25
  • Thanks. I was trying to use modulus at an earlier point when i tested, but i guess i must have gotten that wrong. Works as intended now. – TTT Sep 20 '19 at 14:09