•
•
•
•
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
![]() |
•
•
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation:
Rep Power: 3
Solved Threads: 0
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:
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.
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
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
•
•
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation:
Rep Power: 3
Solved Threads: 0
•
•
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation:
Rep Power: 3
Solved Threads: 0
•
•
Join Date: Aug 2006
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 1
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
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Random number generator's (C++)
- Realy weird random number generator bug (C++)
- Random number generator (Java)
- Random number generator (Java)
- random number generator (C++)
- Help with random number gen (C++)
Other Threads in the Java Forum
- Previous Thread: Change Image based on user Input
- Next Thread: How to use JList


Linear Mode