Results 1 to 6 of 6
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default 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.

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    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.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    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]));
    Hope that makes sense.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    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.

  6. #6
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    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]));
    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 :):) .



    Quote Originally Posted by Norm View Post
    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.

Similar Threads

  1. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 01:57 AM
  2. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 01:26 AM
  3. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 09:28 AM
  4. [SOLVED] Help with math.random
    By tomiu in forum New To Java
    Replies: 10
    Last Post: 12-21-2008, 09:55 PM
  5. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM

Posting Permissions

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