User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 425,998 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,669 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4640 | Replies: 4 | Solved
Reply
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

Random number generator

  #1  
Aug 13th, 2006
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.
Last edited by mrsteve : Aug 13th, 2006 at 1:09 am.
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

Re: Random number generator

  #2  
Aug 13th, 2006
I wrote above "never get anything above 10"

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

sorry for any confusion.
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
Reply With Quote  
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

Re: Random number generator

  #3  
Aug 13th, 2006
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.
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
Reply With Quote  
Join Date: Aug 2006
Posts: 2
Reputation: jigero is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
jigero jigero is offline Offline
Newbie Poster

Re: Random number generator

  #4  
Aug 15th, 2006
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
Reply With Quote  
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

Re: Random number generator

  #5  
Aug 20th, 2006
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
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 12:21 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC