Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java dice roll with unexpected random number

I've written a simple Java program to display the results of 20 dice rolls on the console. The results I'm getting are listed below:

3
1
java.util.Random@62efae3b
1
5
4
1
java.util.Random@62efae3b
1
java.util.Random@62efae3b
java.util.Random@62efae3b
1
6
java.util.Random@62efae3b
1  
java.util.Random@62efae3b
java.util.Random@62efae3b
1
2
3
3

When I ran it for a few times, the string after "@" is different, but basically in the same format. What have I done wrong?

Here is the code:

import java.util.Random;

public class QiProb3 {

    public static void main(String[] args) {

        Random diceNumber = new Random();

        for (int count = 0; count <= 20; count++) {
            if ((diceNumber.nextInt(6) + 1) == 1) {
                System.out.println("1");
            } else if ((diceNumber.nextInt(6) + 1) == 2) {
                System.out.println("2");
            } else if ((diceNumber.nextInt(6) + 1) == 3) {
                System.out.println("3");
            } else if ((diceNumber.nextInt(6) + 1) == 4) {
                System.out.println("4");
            } else if ((diceNumber.nextInt(6) + 1) == 5) {
                System.out.println("5");
            } else if ((diceNumber.nextInt(6) + 1) == 6) {
                System.out.println("6");
            } else {
                System.out.println(diceNumber);
            }
        }
    }
}
like image 997
f_qi Avatar asked Jun 17 '26 02:06

f_qi


2 Answers

else {
    System.out.println(diceNumber);
}

You are printing the address of diceNumber by invoking its default toString() function in your else clause.
That is why you are getting the java.util.Random@62efae3b

The more critical issue is why it gets to the 'else' clause, I believe that is not your intention.
Note: In the question, a new number is generated in each if/else if clause, which is why the code actually gets to the final else clause.

What you should be doing is:

for (int count = 0; count < 20; count++) {

    int rollValue = diceNumber.nextInt(6) + 1;

    if (rollValue == 1) {
        System.out.println("1");
    } else if (rollValue == 2) {
        System.out.println("2");
    } else if (rollValue == 3) {
        System.out.println("3");
    } else if (rollValue == 4) {
        System.out.println("4");
    } else if (rollValue == 5) {
        System.out.println("5");
    } else if (rollValue == 6) {
        System.out.println("6");
    } else {
        // This else is now redundant
        System.out.println(diceNumber);
    }
}

or a more straight-forward method would be:

// count < 20 instead of count <= 20
for (int count = 0; count < 20; count++) {

    int rollValue = diceNumber.nextInt(6) + 1;
    System.out.println(rollValue);
}

Credit goes to 'Elliott Frisch' for realizing that the loop is executed 21 times instead of 20.

like image 195
almightyGOSU Avatar answered Jun 18 '26 14:06

almightyGOSU


You're Re-Rolling

With each if you re-roll the dice. Store the value, and test it!

Random diceNumber = new Random();
for (int count = 0; count <= 20; count++) {
    int roll = diceNumber.nextInt(6) + 1; 
    if (roll == 1) {
        System.out.println("1");
    } else if (roll == 2) {
        System.out.println("2");
    } else if (roll == 3) {
        System.out.println("3");
    } else if (roll == 4) {
        System.out.println("4");
    } else if (roll == 5) {
        System.out.println("5");
    } else if (roll == 6) {
        System.out.println("6");
    } else {
        System.out.println("RNG Error: " + diceNumber);
    }
}

It Could be More Concise

Your posted code might be shortened like

for (int count = 0; count <= 20; count++) {
    int roll = diceNumber.nextInt(6) + 1;
    System.out.println(roll); 
}

Note

Also, you get 21 rolls using the above <= 20 test.

like image 28
Elliott Frisch Avatar answered Jun 18 '26 14:06

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!