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

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

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[5] = 6;

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.

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[5] = 6;

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 ........ :):):):)