Results 1 to 8 of 8
  1. #1
    GerB68 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Can I prevent random numbers from repeating in a 2d array

    Is there a way to Stop repeating random numbers in a 2d array?

    I have a 2d array 5x3 and I need to prevent a random number from repeating in the middle column which is array[r][1]
    Here's what I have...

    public static void main(String[] args)
    {
    int MyArray[][] = new int [5][3];
    fillArray(MyArray);
    OutputArray(MyArray); //method to display array
    }//end of main

    static void fillArray(int array[][])
    {
    for (int r=0; r<array.length; r++)
    {
    for (int c=0; c<array[0].length; c++)
    {
    array[r][c] = (int)(Math.random()*10000)%10+1;
    }//end of column
    }//end of row
    }//end of fillArray

    I can make the element at array[1][1] not equal to array[0][1] but this means alot of coding to ensure all scenarios in this row do not equal each other.
    Is there an easy way?

    Any help greatly appreciated.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default

    Use a Set. Fill the Set up with random numbers until you have enough to fill your array. Then populate your array with those numbers.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    GerB68 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanx for the quick reponse.

    Sorry I'm a noob to Java and I haven't yet come to Set.

    I was hoping for some answer to include a loop, a swap, an if statement or combination of these or something similar.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Try it.
    Post your best effort.
    Ask a specific question about any problem you face.

    db

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default

    In that case, then you already know what you have to do. Write the code that loops through the array up to the current index, and only add the number if it hasn't already been added.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    GerB68 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    "if it hasn't already been added" This is where I'm having problems. I've tried many variations with if statements, while loops and do while loops but I cant seem to identify if an integer has been assigned to a previous element.

    for(int c=1; c<array[0].length-1; c++)
    {
    for(int r=0; r<array.length; r++)
    {
    if (array[r][c] != array[r][c])
    array[r][c] = (int)(Math.random()*10000)%10+1;

    }// end of row for loop
    } // end of column for loop

    Thanx for any help

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default

    Write a method that checks whether a certain item is in the array. Call that method before setting the value of the current index.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    I usually just create a counter that counts from 1 to n add them to an arraylist then do a shuffle on them. Probably not as elegant as the Set method since a set doesnt allow duplicat numbers. But thats how I did it before I discovered Set s

Similar Threads

  1. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 09:33 PM
  2. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 12:59 AM
  3. Non-Repeating Random Integers
    By Psyclone in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 09:04 PM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 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
  •