Clarity...

Reply

Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Clarity...

 
0
  #1
Oct 31st, 2006
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.
  1. public class RollDice
  2. {
  3. /* This program simulates rolling a pair of dice.
  4.   The number that comes up on each die is output,
  5.   followed by the total of the two dice.
  6.   */
  7.  
  8. public static void main (String[] args)
  9. {
  10. int die1; //The number on the first die.
  11. int die2; //The number on the second die.
  12. int roll; //The total roll (sum of the two dice).
  13.  
  14. die1 = (int) (Math.random() *6) + 1;
  15. die2 = (int) (Math.random() *6) + 1;
  16. roll = die1 + die2;
  17.  
  18. System.out.println("The first die comes up " + die1);
  19. System.out.println("The second die comes up " + die2);
  20. System.out.println("Your total roll is " + roll);
  21.  
  22. } //end man()
  23. } //end of class
now this is all okay but when i remove the +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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Clarity...

 
0
  #2
Oct 31st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Clarity...

 
0
  #3
Oct 31st, 2006
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 101
Reputation: cms271828 is an unknown quantity at this point 
Solved Threads: 4
cms271828 cms271828 is offline Offline
Junior Poster

Re: Clarity...

 
0
  #4
Oct 31st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Clarity...

 
0
  #5
Nov 1st, 2006
Thanks guys. And might i add...you guys post replies quite fast! cool!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC