Computer Science Canada

random number

Author:  starlight [ Mon Oct 31, 2005 9:44 pm ]
Post subject:  random number

int dieroll;
dieroll=(int)(6*Math.random())+1

the above lines generate random numbers between 1-6 but i don't really get what the "+1" at the end of the statment is for. anyone can explain? Like if i want to generate a number between 1 and 6 (not including 6) why can't i just simply use "dieroll=(int)(7*Math.random())"

and the other quesiton is

char int d=7
int dd
dd=d++

doesn't d++ mean d=d+1 so shouldn't dd be 8 and d be 7. Why when i tested it means the other way around (d =8 while dd =7)?

and lastly one strange thing that i found in Java is why 3.0/(15/6) equals to 1.5 If 3.0/(15/6) is equal to 1.5 then 15/6 has to equal to 2 meaning the answer will round down. why is that.

Thanks

Author:  beard0 [ Mon Oct 31, 2005 10:10 pm ]
Post subject: 

When you convert (6*Math.random())+1 to an integer, you aren't rounding it, you are truncating it, meaning only if Math.Random returned exactly 1.0 (I'm not sure even if it can) would you end up with a final result of 7. Your second option would have 0 as a possible result.

dd=d++:

d++ returns the value d, and increments d

Author:  Naveg [ Mon Oct 31, 2005 10:12 pm ]
Post subject:  Re: random number

starlight wrote:
int dieroll;
dieroll=(int)(6*Math.random())+1

the above lines generate random numbers between 1-6 but i don't really get what the "+1" at the end of the statment is for. anyone can explain? Like if i want to generate a number between 1 and 6 (not including 6) why can't i just simply use "dieroll=(int)(7*Math.random())"

and the other quesiton is

char int d=7
int dd
dd=d++

doesn't d++ mean d=d+1 so shouldn't dd be 8 and d be 7. Why when i tested it means the other way around (d =8 while dd =7)?

and lastly one strange thing that i found in Java is why 3.0/(15/6) equals to 1.5 If 3.0/(15/6) is equal to 1.5 then 15/6 has to equal to 2 meaning the answer will round down. why is that.

Thanks


1) Math.random() generates a number between 0 and 1. Since the first value you are choosing from is 1, you add 1 (0+1=1)

2)This comes down to prefix and postfix operators. The prefix (++d) will increment the variable and then use the new value in the expression. However, the postfix (d++) first uses the variable and only then increments its value.

3) 15/6 is integer division. In such a case, any decimal is truncated (cut off). Be careful not to confuse this with rounding, 3.778 will become 3! So in this example, 15/6 is 2.5, but because of integer division the decimal is truncated and it becomes 2.

Author:  [Gandalf] [ Mon Oct 31, 2005 10:16 pm ]
Post subject: 

The general format for random numbers in any language is this:

RandomInteger = (int) (Math.random() * maxNumber) + minNumber;

*edit* Oops, right beard0.

Author:  beard0 [ Mon Oct 31, 2005 10:26 pm ]
Post subject: 

[Gandalf] wrote:
The general format for random numbers in any language is this:

RandomInteger = (int) (Math.random() * maxNumber) + minNumber;


errr.....

RandomInteger = (int) (Math.random() * (maxNumber-minNumber+1) + minNumber

Author:  starlight [ Sun Nov 06, 2005 5:03 pm ]
Post subject: 

Thanks. on the other notes. i am pretty confuse with when you can add the arithmetic and character together. for example
why does
code:
char c='A'+1 or
char c='A';c='A'+1 or
char c='A'; c++; works;
while something like
char c='A';
c=c+1; doesn't

Author:  beard0 [ Mon Nov 07, 2005 2:39 pm ]
Post subject: 

starlight wrote:
Thanks. on the other notes. i am pretty confuse with when you can add the arithmetic and character together. for example
why does
code:
char c='A'+1 or
char c='A';c='A'+1 or
char c='A'; c++; works;
while something like
char c='A';
c=c+1; doesn't


It has to do with the way types are interpreted. To be safe, explicitly change types, or use the increment operator:
e.g.
code:
char c = 'A';
c = ((char) (((int) c) + 3)); //for increasing by more than 1 or
c++; //for increasing by 1


: