Solved

# value = (int) (Math.random() * 6.0 + 1.0);

Posted on 2004-11-09
Medium Priority
400 Views
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
0
Question by:mdbbound

LVL 92

Expert Comment

ID: 12540709
because random() returns a value greater than or equal to 0.0 and less than 1.0.
0

LVL 92

Assisted Solution

objects earned 600 total points
ID: 12540714
so Math.random() * 6.0 returns a value greater than or equal to 0.0 and less than 6.0.
which when cast to an integer becomes from 0 to 5

0

LVL 6

Expert Comment

ID: 12540850
>>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
0

LVL 16

Expert Comment

ID: 12541882
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

0

LVL 37

Assisted Solution

zzynx earned 600 total points
ID: 12541959
An alternative for

value = (int) (Math.random() * 6.0 + 1.0);

is

java.util.Random r = new java.util.Random();
value = r.nextInt(6) + 1;      // r.nextInt(6) returns an int from 0 to 5
0

LVL 30

Expert Comment

ID: 12541988
You can think of rounding or ceiling/ flooring the value as per your requirement using the methods in the Math class.
0

LVL 2

Accepted Solution

kjayaraman earned 800 total points
ID: 12543887
Let us make this simple.

Math.random() gives you a random number between 0 and 1. [0 < Math.random() < 1]

Math.random() * 6.0 would be between 0 and 6. [Hey, my dice does not have a 0 !!!]

Note that I specifically mentioned 0 < Math.random() < 1, meaning Math.random() never returns me 0 or 1. It returns me something between it. In other words, with some precision loss,
we can say
0.00001 <= Math.random() <= 0.999999

When I try to convert Math.random()*6.0 into an integer, the decimal part of the number is trimmed, i.e
(int)5.6 = 5
(int)4.1 = 4 [this is what guys call it floor of the number.]

So,
0.00006 <= Math.random()*6.0 <=  5.999994

Now lets typecast to (int)

0 <= (int)(Math.random()*6.0) <= 5 [Note my decimal part gets trimmed when I cast to integer]

Hey, so when I write (int)(Math.random()*6.0), I get a number between 0 and 5. But what I want is a number between 1 and 6. Now you know what to do ;)
0

LVL 37

Assisted Solution

zzynx earned 600 total points
ID: 12544042
>> Note that I specifically mentioned 0 < Math.random() < 1, meaning Math.random() never returns me 0 or 1.
Correction:
I quote from the java docs:
public static double random()
Returns a double value with a positive sign, greater than ***or equal to 0.0*** and less than 1.0
0

Author Comment

ID: 12546164
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.
0

LVL 37

Expert Comment

ID: 12546307
Thanks for appreciating our help
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how ā¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the āforā loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbersā¦
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month17 days, 1 hour left to enroll

#### 862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.