Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Array random numbers

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Array random numbers

    Hi
    I was just wondering if someone could help me a bit in my java code.
    Basically what iv done is made a program that chooses 15 numbers between 1 and 100.
    I was wondering if there was an easier way of writting each array for these fifteen numbers.
    As of now i have written each code individually like this:

                //Array Numbers
                Random randomGenerator = new Random ();
                int num[] = new int[16];
                for (int idx = 1 ; idx < 16 ; idx++)
                    {
                num[idx] = (int)(Math.random()*100);
                    }
                c.println("Here are 15 random numbers between 1 and 100:");
                c.println (num[1]);
                c.println (num[2]);
                c.println (num[3]);
                c.println (num[4]);
                c.println (num[5]);
                c.println (num[6]);
                c.println (num[7]);
                c.println (num[8]);
                c.println (num[9]);
                c.println (num[10]);
                c.println (num[11]);
                c.println (num[12]);
                c.println (num[13]);
                c.println (num[14]);
                c.println (num[15]);
                    }

    The program goes work, i was just interested in finding out whether there was an easier way to put these random number.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array random numbers

    Yes, look at using a for loop. Replace the hardcoded indexes [0] etc with [i] where i is the for loop index.

  3. The Following User Says Thank You to Norm For This Useful Post:

    nitwit3 (July 20th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array random numbers

    Thank You!

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array random numbers

    I have to wonder how you can think to use a loop to insert values into an array but couldn't think to use a loop to print them out.

    int num[] = new int[16];
    If you want an array to hold N number of things then create the array with a size of N. Do not create it with a size of N+1 just to avoid using index 0.
    Improving the world one idiot at a time!

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array random numbers

    yea i know .. im not the brightest light bulb out there -____-

Similar Threads

  1. Array - Random Numbers Progra, Help
    By Jediknock in forum Collections and Generics
    Replies: 2
    Last Post: March 21st, 2011, 10:13 PM
  2. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  3. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 01:54 AM
  4. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM