I'm trying to write a class that randomly generates a number, emulating a dice roll. It runs from the command line (since I have yet to learn any other way of getting user input) taking the number of dice and the type of dice(number of sides). The problem I am having right now is that it never generates the number in the top of the range...here's the code:

import java.util.*; 

public class Roll {

    public static void main(String args[]) {
        if (args.length < 2) {
            System.out.println("Usage: "
                + "java Roll number_of_dice die_type");
            System.exit(-1);
        }
        // this if statement ensures that two arguments
        // are passed into the args array

        int numDie = Integer.parseInt(args[0]); 
        int typeDie = Integer.parseInt(args[1]);
        // turns the string arguments entered at the 
        // command line into integers and assigns
        // them to variables for practical purposes

        int[] result = new int[numDie];
        int total = 0;
        int temp = 0;
        //two variables which will hold the results
        //of each roll and the total of all the rolls 
        
        for (int i = 0; i < numDie; i++) {
            result[i] = getRandom(typeDie);
            //systematically assigns a randomly
            //generated number to each element
            //in the result integer array

            temp = result[i] + 1;
            System.out.println("Roll " + (i + 1) + ":  "
                + (temp));
            total = total + (temp);        
        }
        
        System.out.println("Roll total:  " + total);
    }
    
    // the following method returns a "randomly" generated
    // integer between 0 and n - 1

    public static int getRandom(int n) {
        Random generator = new Random();
        // initiates a Random object to the generator
        // variable

        return generator.nextInt(n - 1);        
    }
}

From my understanding of the Random class, if I use generator.nextInt(n) I should get a number from 0 to n. However, 0 generally isn't on a die so I give it the argument n-1 in anticipation of adding one to the result later (that way if someone wants a six-sided die they will get 1-6 instead of 0-6 when running 'java Roll 1 6'). But no matter how many times I run 'java Roll 1 6' I never get a result of 6. In fact, I never get the top of the range...another example, when I run 'java Roll 10000 10' I never get anything above 10. Maybe I'm missing something here, probably a stupid mistake. If someone could give me another perspective on this, I'd appreciate it very much. Thanks in advance :-D.

Recommended Answers

All 4 Replies

I wrote above "never get anything above 10"

however, I most definitely meant to write "never get anything above 9"

sorry for any confusion.

Ok, I figured it out...the upper bound 'n' in the nextInt(int n) is exclusive so I didn't neet n - 1, just n.

try this

public static int getRandom(int n) {
    Random generator = new Random();
    // initiates a Random object to the generator
    // variable

    return generator.nextInt(n - 1)+1;        
}

because if you let in the parenthesis n, and n=6 you have a dice with 7 sides. you always add 1 so you never has as a result 0

The n in the parenthesis is exclusive...so say you put in 6, you have a random number between 1 and 5. I found this out on the sun microsystems webpage for java :-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.