# Thread: hi guys help me in math.random () in java

1. Senior Member Join Date
Mar 2009
Posts
126
Rep Power
0

## hi guys help me in math.random () in java

hi guys ,

I am learning java from head first java ,so i finished till 53 pages with any problem but after that i come across one tough program where i couldn't able to understand the logic of math.random () . so please i have taken the image of this program from PDF and i have hosted it down , so please see the below image containing program and explain me clearly the logic of math.random() which is the 3rd step in that program...thanks in advance Last edited by funkygarzon; 08-08-2010 at 09:03 PM.  Reply With Quote

2. ##   Reply With Quote

3. Senior Member Join Date
Mar 2009
Posts
126
Rep Power
0

##  Originally Posted by alacn thank you very much for the quick reply dude ,

Actually dude i know the random method will produce a random number , but my doubt is , please look at the below code i.e., "3"rd step of the above program

Java Code:
```Generate three random number
int rand1  - (int) (Math.random() * oneLength);
int rand2  - (int) (Math.random() * twoLength);
int rand3  - (int) (Math.random() * threeLength);```
how does the above code produce the random result ? i am confused only with the multiple sign (*) in between Math.random and onelength . so please help me in explaining the logic dude ? :confused::confused::confused::confused: thanks in advance
Last edited by funkygarzon; 08-08-2010 at 09:39 PM.  Reply With Quote

4. ## Let's imagine that you have ten elements in some array:
int[] ourArray = {1,2,3,4,5,6,7,8,9,10}
Now we know that Math.random() produces a number where 0 <= x < 1. So, let's say our random number picker picks 0.561. This doesn't help us really, because there is no element number 0.561. So what we do instead, is we want to pick a number that satisfies: 0 <= x < 10. (So it will pick an index from 0 to 9.)

In order to change the 0 <= x < 1 to 0 <= x < 10, we have to multiply the random number by ten. In our case, that's 5.61. Now, obviously, we can't pick element 5.61 from the array... so we cast it to an integer (i.e. the (int) that you see), which converts it to the number 5. Now, when we select element 5 from our array, we get:
ourArray = 6;

Put it all together, and it looks like this:
Java Code:
```int[] ourArray = {1,2,3,4,5,6,7,8,9,10}; // Array of numbers.
double randomNumber = Math.random(); // This is the number 0.0 <= x < 1.0 .
randomNumber *= ourArray.length; // This is equal to 10. Now randomNumber is 0.0 <= x < 10.0 .
randomInt = (int)randomNumber; // Converts it from a double to an integer (5.61 to 5).
System.out.printf("We selected element %d, which is %d",randomInt,ourArray[randomInt]));```
Hope that makes sense.  Reply With Quote

5. ## i am confused only with the multiple sign (*) in between Math.random and onelength
Write a small test program and put this statement in it with a
System.out.println("rand1=" + rand1) following it to show the results. Compile and execute it.
Go back and change what following the * and do it again. Compare the results to the first program.  Reply With Quote

6. Senior Member Join Date
Mar 2009
Posts
126
Rep Power
0

##  Originally Posted by Zack Let's imagine that you have ten elements in some array:
int[] ourArray = {1,2,3,4,5,6,7,8,9,10}
Now we know that Math.random() produces a number where 0 <= x < 1. So, let's say our random number picker picks 0.561. This doesn't help us really, because there is no element number 0.561. So what we do instead, is we want to pick a number that satisfies: 0 <= x < 10. (So it will pick an index from 0 to 9.)

In order to change the 0 <= x < 1 to 0 <= x < 10, we have to multiply the random number by ten. In our case, that's 5.61. Now, obviously, we can't pick element 5.61 from the array... so we cast it to an integer (i.e. the (int) that you see), which converts it to the number 5. Now, when we select element 5 from our array, we get:
ourArray = 6;

Put it all together, and it looks like this:
Java Code:
```int[] ourArray = {1,2,3,4,5,6,7,8,9,10}; // Array of numbers.
double randomNumber = Math.random(); // This is the number 0.0 <= x < 1.0 .
randomNumber *= ourArray.length; // This is equal to 10. Now randomNumber is 0.0 <= x < 10.0 .
randomInt = (int)randomNumber; // Converts it from a double to an integer (5.61 to 5).
System.out.printf("We selected element %d, which is %d",randomInt,ourArray[randomInt]));```
Hope that makes sense.

woooooooooooooooooooooooooooooooooooooooooooow great and superb logic dude , thank you very much , you are doing wonderful job dude ...keep it . It is god who made me to find this wonderful forum for me to learn java , Thanks and glory to my god and savior jesus christ :):) . Originally Posted by Norm Write a small test program and put this statement in it with a
System.out.println("rand1=" + rand1) following it to show the results. Compile and execute it.
Go back and change what following the * and do it again. Compare the results to the first program.

yes dude thank you very much for your reply dude , of course i did saw that result first as you said ,but i wanted to know the exact logic behind this Math.random() and now "Zack" made me clea .thank you ........ :):):):)

:):):):)
Last edited by funkygarzon; 08-09-2010 at 04:54 AM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•