Results 1 to 8 of 8
- 04-20-2011, 01:57 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-20-2011, 02:04 PM #2
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!
- 04-20-2011, 02:12 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-20-2011, 02:15 PM #4
Try it.
Post your best effort.
Ask a specific question about any problem you face.
db
- 04-20-2011, 02:16 PM #5
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!
- 04-20-2011, 04:20 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
"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
- 04-20-2011, 05:01 PM #7
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!
- 04-22-2011, 11:40 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Array of random numbers ...
By shane1987 in forum New To JavaReplies: 31Last Post: 11-14-2010, 09:33 PM -
Store Random numbers into Array
By abby0910 in forum New To JavaReplies: 19Last Post: 07-12-2010, 12:59 AM -
Non-Repeating Random Integers
By Psyclone in forum New To JavaReplies: 5Last Post: 01-31-2010, 09:04 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 03:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks