Results 1 to 6 of 6
 08082010, 09:59 PM #1Senior 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; 08082010 at 10:03 PM.
 08082010, 10:11 PM #2Teaching myself java so that i can eventually join the industry! Started in June 2010
 08082010, 10:33 PM #3Senior Member
 Join Date
 Mar 2009
 Posts
 126
 Rep Power
 0
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);
Last edited by funkygarzon; 08082010 at 10:39 PM.
 08082010, 11:05 PM #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[5] = 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]));
 08082010, 11:38 PM #5i am confused only with the multiple sign (*) in between Math.random and onelength
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.
 08092010, 05:48 AM #6Senior Member
 Join Date
 Mar 2009
 Posts
 126
 Rep Power
 0
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 :):) .
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; 08092010 at 05:54 AM.
Similar Threads

Math.random not zero
By rsvr in forum New To JavaReplies: 3Last Post: 02282010, 02:57 AM 
Math.random
By p0rnstar in forum New To JavaReplies: 9Last Post: 01272010, 02:26 AM 
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09142009, 10:28 AM 
[SOLVED] Help with math.random
By tomiu in forum New To JavaReplies: 10Last Post: 12212008, 10:55 PM 
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11232007, 03:09 PM
Bookmarks