Results 1 to 7 of 7
  1. #1
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default How do you store a random integer in an array?

    I have a program which requires me to randomly generate 25 integers from 0-99 using an array. I don't really know where to start. I thank anyone who helps me.

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

    Default Random()

    The way to do this would be:
    1- use a "for" loop
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    2- Within the "for" loop, generate the random number
    Random (Java Platform SE 6)
    Java Random Numbers
    3- Populate the array with each random number generated:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL

    PS:@carderne: please DO NOT suggest things you don't know anything about:
    • RandInt method doesn't exist in Java
    • The Integer class doesn't have a rand() method either.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    i done this before.
    hope this may help you.

    Java Code:
    import java.util.*;
    int a = (int) (Math.random()*100);
    multiple with numbers will affect your result
    *10 -> 0-9
    *100 -> 0-99

    because we want to round up the numbers, so we use int.
    from double,we down cast it to int. (int)

    PS:actually all this things can be found at any search engine.
    (and that is not really hard to find the related source)
    if you type "generate random numbers",
    you may find some many related articles and sources.
    Last edited by angelicsign; 12-16-2008 at 05:20 AM.

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

    Default If you want to help...

    @carderne: if you really want to help, then please put some effort into it. Sending someone on a wild goose chase is not very helpful. If you can't remember what the name of the method is, then take the effort look it up. Integer.rand() is in no way or form even close to math.random().
    We all appriaciate any help we can get, but it has to be the right help.

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

  5. #5
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    You're right and I apologise. My only excuse being that I was on my phone with no access to a computer...

    Unruly posts duly removed.

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

    Default Thanks

    Apology accepted. We're in sync. Thanks for the help.

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

  7. #7
    Tankster is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Smile Generating random numbers

    Hi this is the code to generate 5 random numbers and then put them into an array. If the same number is produced twice then the second one is ignored and then the procedure repeated. Hope this helps. To generate the random number up to 99 use Math.random()*100, which is equal to 0 - 100-1 which is 99.


    public class RandomArray
    {
    public static void main(String[] args)
    {
    //This is the array to hold the random numbers
    int [] numbers = new int [5];
    //This is the variable that will hold the random number each time it is created
    int num;
    //This boolean will hold the value if there is a number being put into the array. If there is then it cancels
    //the first do while loop and then creates the next random number
    boolean numberIn = false;
    //This is the variable that is incremented each time there is a number put into the array. This also cancels the outer do while loop
    //when the count is more than numbers.lenght-1 which is greater than 4. The indexes in the array range from 0-4
    int count;

    //This loop is used to read in -1 to each element in the array. This is because the element is filled by 0's by default
    //If we dont prefill it then it makes it harder if one of the random numbers to be put into the array is 0
    for (int loop = 0; loop<numbers.length; loop++)
    { numbers[loop]=-1;
    System.out.println(numbers[loop]);
    }//for

    //This outer do while is to generate the random number. When it is created count is initialised to 0 and NumberIn false
    //Then the inner loop goes through each index in the array to see if it is -1 which means no number has been put into that
    //index. If the number is not -1 and it is not the same as the number we are trying to put into the array then we go to
    //the next index in the array and see if its empty. This process is repeated until all the array indexes are full and then ends
    //the process
    do
    {
    num =(int)( Math.random()*10);
    count = 0;
    numberIn = false;


    do
    {

    //If the value in the array index is the same as the number trying to put in then exit the loop to generate another number
    if (numbers[count]==num)
    numberIn = true;
    //If the array index is empty(It is -1 which we prefilled the array with) put the number in and then exit this loop
    else if (numbers[count]==-1)
    { numbers[count]=num;
    numberIn = true;
    }//else if
    else
    //Otherwise go to the next index in the array and then start the inner do while again
    count++;
    }while(!numberIn);//While we have not put a number into the array continue

    }while(count!=4);//while the array index has not reached the last one

    //print out the index of the array just to test and see the values
    for (int trip = 0; trip<numbers.length; trip++)
    System.out.println(numbers[trip]);

Similar Threads

  1. Store String Tokenizer o/p onto an array
    By rajchief in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 10:00 PM
  2. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 04:57 AM
  3. Replies: 3
    Last Post: 04-07-2008, 07:02 PM
  4. questions about using array to store profile
    By hien_NU in forum New To Java
    Replies: 6
    Last Post: 01-08-2008, 06:03 AM
  5. Replies: 2
    Last Post: 12-12-2007, 06:01 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
  •