Results 1 to 7 of 7
  1. #1
    Jadles is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default Multiple Random Numbers

    I know how to make a method that creates a single random number, but I'm not sure how to make one that prints out multiple random numbers based on the parameter (int howMany). Here's my code for one random number:

    Java Code:
        public void printOneRandom()
        {
            int index = randomGenerator.nextInt();
            System.out.println(index);
        }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Use a loop.

  3. #3
    Jadles is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Do you think you could expand on that. I figured you had to use a loop but just knowing I have to use a loop doesn't help all that much.

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

    Default

    Which loop works best when you know in advance how many times you want to loop (and here you do, since that number will be passed as a parameter to the method)?

    Use that loop, and try to solve this. Come on, let's see an attempt by you first. This way we'll know exactly what problems you are having.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Loopy di loop

    Some pseudo code:
    Java Code:
    public void printManyRandomNums(int num)
        {
             start loop (using variable num)
             int index = randomGenerator.nextInt();
             System.out.println(index);
             end loop
        }
    Some links to tutorial about loops:
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Jadles is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks CJSLMAN, the tutorials really helped.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default no problem...

    Welcome...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 06:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 03:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 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
  •