mdbbound
asked on
value = (int) (Math.random() * 6.0 + 1.0);
Hello,
I have this homework to roll 2 dice and there is a part of the code below that i just dont get.
Can someone please help me figure out why we have to have the +1 in this Math.Random.
value = (int) (Math.random() * 6.0 + 1.0);
The six means that there are 6 different possible number of dots, i don't know what the + 1 is for.
Thanks
I have this homework to roll 2 dice and there is a part of the code below that i just dont get.
Can someone please help me figure out why we have to have the +1 in this Math.Random.
value = (int) (Math.random() * 6.0 + 1.0);
The six means that there are 6 different possible number of dots, i don't know what the + 1 is for.
Thanks
because random() returns a value greater than or equal to 0.0 and less than 1.0.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Can someone please help me figure out why we have to have the +1 in this Math.Random.
to make the possible values of "value" from 1 to 6, probably don't want to have 0 value.
(Math.random() * 6.0 + 1.0
converts it to next integer
example
if value of (Math.random() * 6.0 is 0.6
when it is added to 1 it becomes 1.6
then finally value = (int)1.6 is 1
the possible values of "value" will be
1 to 6
to make the possible values of "value" from 1 to 6, probably don't want to have 0 value.
(Math.random() * 6.0 + 1.0
converts it to next integer
example
if value of (Math.random() * 6.0 is 0.6
when it is added to 1 it becomes 1.6
then finally value = (int)1.6 is 1
the possible values of "value" will be
1 to 6
Hi mdbbound,
Well, I think that all the explanation above should be straight forward. So if you have codes like this.
value = (int) (Math.random() * 6.0);
It will return the value from 0.0 to 5.0. It will not touch 6.0 UNLESS you add it by 1.0. So in the roll game, there is no value for the dice to be zero, so the value can only between 1 to 6. Did you get it?
So, if you need to have the random number between 10 and 200. You will have codes like
value = (int) (Math.random() * 190 + 10);
Hehe, now you know already right? Please don't hesitate to ask more questions
David
Well, I think that all the explanation above should be straight forward. So if you have codes like this.
value = (int) (Math.random() * 6.0);
It will return the value from 0.0 to 5.0. It will not touch 6.0 UNLESS you add it by 1.0. So in the roll game, there is no value for the dice to be zero, so the value can only between 1 to 6. Did you get it?
So, if you need to have the random number between 10 and 200. You will have codes like
value = (int) (Math.random() * 190 + 10);
Hehe, now you know already right? Please don't hesitate to ask more questions
David
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can think of rounding or ceiling/ flooring the value as per your requirement using the methods in the Math class.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello everyone,
Thank you so much for all the responses. I'm just starting to learn java, i love it but it's kinda difficult.
I wanted to give the points to objects just as soon as i posted the question for his quick response but somehow i had a problem with my internet connection.
So for now i will give the points to Objects. But I still have some more questions on some parts of the code in my other lectures coz i really want to understand java.
zzynks and kjayaraman, Thank you for an in-depth explanation, i truly appreciate it.
Thank you so much for all the responses. I'm just starting to learn java, i love it but it's kinda difficult.
I wanted to give the points to objects just as soon as i posted the question for his quick response but somehow i had a problem with my internet connection.
So for now i will give the points to Objects. But I still have some more questions on some parts of the code in my other lectures coz i really want to understand java.
zzynks and kjayaraman, Thank you for an in-depth explanation, i truly appreciate it.
Thanks for appreciating our help