| | |
Clarity...
![]() |
•
•
Join Date: May 2006
Posts: 69
Reputation:
Solved Threads: 1
hi just have a question to make sure i understand it. i'm doing the exercises in my book and it says i should create a program which will roll 2 dice and produce a random number then adds the sum of the outcomes.
now this is all okay but when i remove the
Thanks
Java Syntax (Toggle Plain Text)
public class RollDice { /* This program simulates rolling a pair of dice. The number that comes up on each die is output, followed by the total of the two dice. */ public static void main (String[] args) { int die1; //The number on the first die. int die2; //The number on the second die. int roll; //The total roll (sum of the two dice). die1 = (int) (Math.random() *6) + 1; die2 = (int) (Math.random() *6) + 1; roll = die1 + die2; System.out.println("The first die comes up " + die1); System.out.println("The second die comes up " + die2); System.out.println("Your total roll is " + roll); } //end man() } //end of class
+1 there isnt any difference. i just want to know if removing the +1 will cause any problem on the program. if not...how about if i'm writing a longer program, will there be any implication?Thanks
if take out +1 this will generate numbers in the interval between 0-5 inclusive
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Oct 2006
Posts: 95
Reputation:
Solved Threads: 3
Yeh, thats right, to be precise...
Math.random() generates a number between 0 and 1 inclusive, the chance of getting 0 or 1 is almost 0 though.
so suppose you wanted 1 to 6, first you mulitply by 6,
this gives you a number in the range 0-6, eg 5.92367236746.
Then converting to an int, will always give you either 0,1,2,3,4,5.
So just add 1, to get 1 to 6.
Math.random() generates a number between 0 and 1 inclusive, the chance of getting 0 or 1 is almost 0 though.
so suppose you wanted 1 to 6, first you mulitply by 6,
this gives you a number in the range 0-6, eg 5.92367236746.
Then converting to an int, will always give you either 0,1,2,3,4,5.
So just add 1, to get 1 to 6.
![]() |
Similar Threads
- fstream Tutorial (C++)
- cannot find server or DNS error (Web Browsers)
- SYSTEM.INI missing on WIN 98 SE (Windows 95 / 98 / Me)
- system32\bridge.dll error on start-up (Viruses, Spyware and other Nasties)
- error message on startup: bridge.dll missing (hijackthis log inside) (Viruses, Spyware and other Nasties)
- IE 6x Advanced Tab -> Settings Blank (Web Browsers)
- Can i do something international using which side of vb.net whether it is ado.net, we (VB.NET)
- Turn on ClearType Font-Rendering Technology (Windows tips 'n' tweaks)
- Restarting a windows 2000 computer from the command line (Windows NT / 2000 / XP)
- Is my monitor going? (Monitors, Displays and Video Cards)
Other Threads in the Java Forum
- Previous Thread: Appearing for Java certification
- Next Thread: need help concerning java
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide idea image int j2me j2seprojects java javac javaee javame javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






