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

    Default

    So far my code is this. And the trials seem to be generating the correct numbers. Correct me if I am wrong please! And I greatly appreciate your help.
    Java Code:
        int num=(int) (Math.round(Math.random()*10));
        System.out.println(num);
        int num1=(int) (Math.floor((30-0)*Math.random()) + 1);
        System.out.println(num1+1);
        int num2=(int) (Math.floor((70-29)*Math.random()) + 30);
        System.out.println(num2+1);

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

    Default

    A way to test your usage of Math.random would be to use it in a loop, count the results and print them out to see. Write a small program for testing with the following:
    Java Code:
    int[] counter = new int[100];   // give enough elements for range of number generated
    for(int i = 0; i < 100000; i++)  {
      int num=(int) (Math.round(Math.random()*10));
      counter[num]++; // count number of times this value 
    }
    System.out.println("counter=" + Arrays.toString(counter)); // show counts

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    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);
    That is statistically incorrect; better use this:

    Java Code:
    int num= (int)(Math.random()*30)+1;
    kind regards,

    Jos
    Last edited by JosAH; 09-25-2010 at 04:29 PM.

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

    Default

    To see the problem run the code I posted and you get:

    //counter=[5102, 9965, 9928, 10027, 10135, 10077, 9983, 9947, 9945, 9921, 4970, 0] 11 values using rounded

    The distribution of the counts should be approximately the same. The first of 5102 and eleventh value of 4970 are too low because of the rounding.

    With JosAH's code you get:

    //counter=[10159, 10002, 9861, 10053, 10000, 9931, 9844, 9956, 10090, 10104, 0, 0] 10 values w/o rounded

    Notice the flat distribution of the counts
    Last edited by Norm; 09-25-2010 at 03:11 PM.

  5. #25
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Mabe a bit easier explanation is this:

    Java Code:
    (int)Math.round(Math.rand()*n)
    ... only returns the number 0 for the (pseudo) random numbers in the range [0, 0.5); for numbers in the range [0.5, 1.5) the result will be equal to 1 so the probability is twice as much. Similar results apply for the number n-1 (also a probability that is twice too low).

    kind regards,

    Jos

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

    Default

    I don't need to account for probability, but will my code work? because we haven't used some of the things you guys are talking about.
    Thanks for the tips!

  7. #27
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jdicerch View Post
    I don't need to account for probability, but will my code work? because we haven't used some of the things you guys are talking about.
    Thanks for the tips!
    Ok, summarizing, when you want a number in the range [0, n) exclusive, you can do:

    Java Code:
    (int)(n*Math.rand())
    consequently when you want a number in the range [m, n) you can generate a number in the range [0, n-m) and add m afterwards, as in:

    Java Code:
    int((n-m)*Math.rand())+m
    Forget about rounding, as I said, you'll introduce statistical inaccuracies.

    kind regards,

    Jos

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

    Default

    I need it to be inclusive not exclusive. What would be the command for that?

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jdicerch View Post
    I need it to be inclusive not exclusive. What would be the command for that?
    Add one to the value of n? That wasn't so difficult was it?

    kind regards,

    Jos

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

    Default

    I need it to be inclusive not exclusive. What would be the command for that?
    You can determine the values returned by writing a small test program and executing it, looking at the output and seeing if it generates what you want. See my code above.
    If it doesn't give you what you want, change it and do it again. Continue until you get the desired results.

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

    Default

    so (int)((n+1)*Math.rand())

    And I got one more part to this. Your second job is to place the following line in your code: String output = "Your~generated~variables~are~";

    You must use the proper function found within the String class to replace each tilde in this string with a space, append the integers created in Part `1 onto the end of the string, and then print it out. You MUST NOT simply reassign (without using the proper String method) the output string in order to get rid of the tildes, i.e.:
    output="Your generated variables are"; (<--DONT DO THIS)

    I have been fooling around with this code but it doesnt seem to be working in my favor:

    Java Code:
        /*Scanner console = new Scanner(System.in);
        String gener=("Your~generated~variables~are~");
        gener = console.nextLine();
        gener=gener.replace("~"," ");
        System.out.println(gener+num+num1+num2);*/

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

    Default

    and when i did this:

    Java Code:
    int[] counter = new int[100];   // give enough elements for range of number generated
    for(int i = 0; i < 100000; i++)  {
      int num=(int) (Math.round(Math.random()*10));
      counter[num]++; // count number of times this value 
    }
    System.out.println("counter=" + Arrays.toString(counter)); // show counts
    I get this error message:
    Java Code:
    Error: Arrays cannot be resolved

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

    Default

    Did you look up the Arrays class in the Java API documentation? It will show you what package the Arrays class is in. Then add an import statement for that package to your code.

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

    Default

    I did and i either could import java.lang.Object, or java.lang.reflect.

    Neither work.

  15. #35
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jdicerch View Post
    I did and i either could import java.lang.Object, or java.lang.reflect.

    Neither work.
    Class Arrays is in the java.util package so either import the entire package (import java.util.*) or just the class you need (import java.util.Arrays).

    kind regards,

    Jos

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

    Default

    Did you look at the API doc for the "Arrays" class? The line above where it says:
    Class Arrays
    has the package.

    You MUST know how to look things up in the API doc if you want to write java programs.

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

    Default

    Perfect. Thank you!
    Would you be able to help me with the second part?
    Thanks again

  18. #38
    s0meb0dy is offline Member
    Join Date
    Sep 2010
    Posts
    42
    Rep Power
    0

    Default

    Random oRandom = new Random();
    int num1 = oRandom.nextInt(21); //would show 0 to 20
    int num2 = oRandom.nextInt(21)+1;//would show 1 to 20
    int num3 = oRandom.nextInt(41)+30;//would show 30 to 70 including both 30 and 70
    int num4 = oRandom.nextInt(40)+31;// would show 31 to 69

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

    Default

    I got the first part down, can anyone help me with the second? : /

  20. #40
    s0meb0dy is offline Member
    Join Date
    Sep 2010
    Posts
    42
    Rep Power
    0

    Default

    String output = "Your~generated~variables~are~";
    System.out.println(output+num1+" and "+num2);//would show Your~generated~variables~are~ num1 and num2 (num1 and num2 being a random number)

Page 2 of 3 FirstFirst 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
  •