Page 1 of 3 123 LastLast
Results 1 to 20 of 51
  1. #1
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default Trying to write my own program : /

    Okay well I am practicing for a test and our teacher gave us some practice problems. I have to create random numbers using the static random() method. Part1 asks me to create 3 integers. one between 0 and 20, another between 1 and 30, and a third between 30 and 70. I also need to store these values for later use.

    so far i have this (dont make fun, im new to this):

    import java.lang.Math;
    import java.util.Scanner;

    public class ranNum
    {
    public static void main(String[] args)
    {
    Scanner console = new Scanner(System.in);
    int a=((int)(Math.random()*10));
    double b=Math.random();
    double c=Math.random();
    //System.out.println("Please input:");
    a = console.nextInt();
    System.out.println(+a);
    }
    }


    i know a lot is wrong, i am just doing trial and error.

    Thanks!
    Joey

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    First you need to work out the math for taking the output of the random method and mapping it to a number in the ranges you want.
    Do you have the logic for how to do that?
    There are two parts to the logic. One is to multiply the value returned by random to get one of the possible numbers in the range. For example if the range is 10 to 19 inclusive there are 10 possible values: 0 to 9.
    The next part of the logic is to adjust the range of numbers so it maps to the actual range you want. For a range of 10 to 19 you would need to add 10 to the value from the multipling part (which gave you a number from 0 to 9) to map it to 10 to 19.

  3. #3
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    I want whole numbers as well. Would i do something like Math.round(Math.random()*10) for 0-10?

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Not sure if Math.random() is a total requirement but this is a little less complex to figure out:

    Random (Java 2 Platform SE v1.4.2)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    I want whole numbers
    You could cast the double to an int by adding (int)

  6. #6
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    I was told to use Math.random. Norm you think i should do: (int)(Math.round(Math.random()*10))?

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    if you want numbers between 0 and 10 then yes use that, multiply by 20 for numbers between 0 and 20.

    for between 1 and 30 multiply by 29 and add 1.
    e.g
    Java Code:
    int num=(int) (Math.round(Math.random()*29));
    System.out.println(num+1);

  8. #8
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Works perfect but when i go to do my second number using: int num1=(int) (Math.round(Math.random()*29+1));
    I get this error:The local variable num1 is never read

  9. #9
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Actually it's working now. Is this correct? : int num=(int) (Math.round(Math.random()*10));
    System.out.println(num);
    int num1=(int) (Math.round(Math.random()*29));
    System.out.println(num1+1);
    int num2=(int) (Math.round(Math.random()*69));
    System.out.println(num2+1);

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by jdicerch View Post
    Actually it's working now. Is this correct? : int num=(int) (Math.round(Math.random()*10));
    System.out.println(num);
    int num1=(int) (Math.round(Math.random()*29));
    System.out.println(num1+1);
    int num2=(int) (Math.round(Math.random()*69));
    System.out.println(num2+1);

    yes, that is fine, you could have used the variable num throughout aswell just by reassignment e.g.

    Java Code:
    int num=(int) (Math.round(Math.random()*10));
        System.out.println(num);
        num=(int) (Math.round(Math.random()*29));
        System.out.println(num+1);
        num=(int) (Math.round(Math.random()*69));
        System.out.println(num+1);
    I hope you understand the code... Math.random() produces a number between 0.0 and 1.0 if you multiply this numbet by 10 as in your first piece of code you could have something like 0.8735 * 10 = 8.735 which is then rounded to a whole number by Math.round(). to get numbers that begin with 1 and the target number inclusive you must multiply by the desired high number minus 1 e.g. (1-30 multiply by 29) the add by 1 to eliminate 0 and include the high number e.g. 29+1=30. hope this helps.
    Last edited by al_Marshy_1981; 09-25-2010 at 03:26 AM.

  11. #11
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    I have to keep it stored for later use : /

    Here were the complete directions, can you tell me if this fulfills them: Your job is first to generate three integers. The first should be between 0 and 20(inclusive), the second between 1 and 30(inclusive), and the third 30 and 70(inclusive). In the course of doing this, you should store your generated integers in variables for future use.

    Here is my complete code:

    Java Code:
    import java.lang.Math;
    import java.util.Scanner;
    
    public class ranNum
    {
      public static void main(String[] args)
      {
        Scanner console = new Scanner(System.in);
        int num=(int) (Math.round(Math.random()*10));
        System.out.println(num);
        int num1=(int) (Math.round(Math.random()*29));
        System.out.println(num1+1);
        int num2=(int) (Math.round(Math.random()*69));
        System.out.println(num2+1);
      }
    }

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    not quite

    between 0 and 20 is very similar to 0 and 10...
    you have 1 and 30 correct
    you got confused with 30 and 70..... your base number is 30 not 0, remember Math.random() gives a number between 0.0 and 1.0, you need to increase that 0.0 compensate for 30 somehow

    think about it some more then get back to me with your best attempt.

  13. #13
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Grr haha I'm struggling. Would I use this command? (Math.floor(Math.random()*11))
    Or am I way off?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No. Just use Math.random() but you'll need to multiply it by something and then add something to it. This is nothing more than very basic algebra. Figure it out on paper first. For instance in your last example, 0 will map to 30 and 1 will map to 70. So if 0 is X, Y will be 30 and then if X is 1, Y is 70. Knowing this, figure out M and K:

    Y = M*X + K

  15. #15
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    So for 0-10 Math.random()*29+30? Or the other way around? I understand what you're saying but I can't comprehend where to put it in the equation : /

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    quick tip for getting 0 and 20. 1st read my post #10, then seriously get out your calculator and multiply any number between 0.0 and 1.0 by 20 and see the result, then round the result, it should give you a good insight into what Math.floor() is doing to Math.random(). You will notice the further you come to 1.0 the closer you will get to 20 and the closer you come to 0.0 the nearer your answer is 0.

    try multiplying 0.95 by 20....

  17. #17
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Could you maybe explain why the code is wrong? I compiled it and it does generate whole numbers between 0-20, 1-30, 30-70. Or is it not including 0 or 70 or something? Sorry for being a pain in the butt.

  18. #18
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    int num=(int) (Math.round(Math.random()*10));
        System.out.println(num);
    there is no way this is producing any number higher than 10, so to say you are getting a number between 0-20 for it could not be true. as for the others Math.random() produces exactly that, a random number, run the program a few times and I guarantee that the code you produced for 30-70 will produce a number below 30 eventually.

  19. #19
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Tell me if i am right or wrong. my Math.random()*10 is giving me a number from 0-10.
    *29 is giving me a number from 0-29. and *69 is giving me a number from 0-69? So you're saying I need to set the base(where my random number will start). Right? or no

  20. #20
    jdicerch is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

Page 1 of 3 123 LastLast

Similar Threads

  1. which classes to write..how to program this?
    By sony1 in forum New To Java
    Replies: 7
    Last Post: 09-04-2010, 06:12 PM
  2. How to write two server program in java
    By priyamurugar in forum Networking
    Replies: 1
    Last Post: 04-27-2010, 11:20 AM
  3. is it possible to write program with out thread
    By makpandian in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-21-2008, 05:41 PM
  4. How to write interceptor program in struts2?
    By vasu in forum Web Frameworks
    Replies: 1
    Last Post: 10-07-2008, 07:53 AM
  5. need help to write Program in JAVA
    By maneibr in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 01:28 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
  •