0

Why isn't variable a and b getting any values? I have done so that a 100 sided dice with the numbers 1-100 is rolled 50.000 times and I wanna see how often it gives the number 1 and 100 but it isn't getting any value. The result still comes out as a= 0 and b = 0.

package Atest;
import javax.swing.*;

public class Tärning5{
    public static void main(String[] arg){

        int rolls;
        double dice = (int) (Math.random()*100) + 1;
        int n = 0;
        int b=0, a=0;

        for(rolls=50000; n<=rolls; dice = (int) (Math.random()*100) + 1) {
            n++;

            if(dice == 100) 
                a = a++;
            else if (dice == 1) 
                b = b++;
        }

                JOptionPane.showMessageDialog(null, "Dice rolls " + rolls + " times"
                        + "\n"
                        + "\nDice landed on 100 " +a+" times"
                        + "\nDice landed on 1 "+b+" times");  
         }
    }

Aaravos
  • 1
  • 3
  • 3
    Save it in int instead of double. Also see [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). This is the reason why `dice == 100` will probably be never true even if dice value is 100.00 – Yoshikage Kira Sep 22 '19 at 17:49
  • Possible duplicate of [Decrement in Java](https://stackoverflow.com/questions/11086765/decrement-in-java) – Ivar Sep 22 '19 at 17:51
  • 1
    replace `double dice = ...` with `int dice = ...` – Lev Leontev Sep 22 '19 at 17:52
  • It won't work with ```int dice = Math.random()*101;``` because the type Math.random() is a float. It gives a value x where 0<= x < 1 – Aaravos Sep 22 '19 at 17:56
  • 3
    Don't assign `a++` back to `a`. Just `a++;` instead of `a = a++;`. (And the same for `b`.) Other than that your code should work fine. – Ivar Sep 22 '19 at 17:56
  • Wow nice @Ivar It totally solved! But why ? I don't understand. – Aaravos Sep 22 '19 at 17:57
  • @Aaravos That is explained in the link in my first comment. (That link is about decrement instead of increment, but the rules are the same.) – Ivar Sep 22 '19 at 17:58
  • Actually, this might be a better one: [What is x after “x = x++”?](https://stackoverflow.com/questions/7911776/what-is-x-after-x-x) – Ivar Sep 22 '19 at 18:01

1 Answers1

0

I would advise using java.util.Random for generating a random number. You can also simplify your loop so it would be more straightforward.

    int a = 0;
    int b = 0;
    int rolls = 50000;
    Random random = new Random();
    for (int i = 0; i < rolls; i++) {
        int dice = random.nextInt(100) + 1;

        if (dice == 100) a += 1;
        if (dice == 1) b += 1;
    }

    System.out.println("a: " + a + ", b: " + b);

The conditions in the "for" statement do not refer to anything other than the iteration count. Inside the loop we can "simulate" a dice roll by using existing Random to create a next random int (in this example: in the range from 1 to 100 inclusively).

Example output:

a: 502, b: 475
multicatch
  • 192
  • 4