Results 1 to 4 of 4
  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Math.random() method problem

    I'm trying to make a basic program that will print out 3 random numbers using the Math.random() method...

    public class Basic{
    public static void main(String[] args)
    {
    for (int j=0; j<3;j++){
    Math.random();
    System.out.println(j);
    }
    }
    }

    Right now it prints 0 1 2 in a column. But I need it to print random numbers. I can't figure out what to do with the Math.random() to make it work...

  2. #2
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Math.random() method problem

    Replace your for loop with this one:

    Java Code:
    for (int j=0; j<3;j++){
    		int num = (int)(Math.random() * (1000)); // Range of 1,000
    		System.out.println(num);
    		}

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Math.random() method problem

    Thank you! :)
    I get it...

  4. #4
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

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. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 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
  •