Results 1 to 3 of 3
  1. #1
    katie is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default math.random function help

    How can I get random numbers in the set 2, 4, 6, 8, 10 using a single statement of Math.random?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    for(int j = 0; j < 40; j++) {
        int n = (((int)(j * Math.random())+2)/2)*2;
        System.out.printf("%2d    ", n);
        if((j+1) % 8 == 0)
            System.out.println();
    }

  3. #3
    katie is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

Similar Threads

  1. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 04:06 PM
  2. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  3. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  4. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 AM
  5. Date math
    By orchid in forum New To Java
    Replies: 2
    Last Post: 04-18-2007, 07:01 AM

Posting Permissions

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