Hi everyone I am new here. I am having trouble with this program. I can get the 2D array to print out the way I want it to but I can't have any repeats for the lottery numbers. I have tried about a hundred different ways to not create repeats. I have also tried creating a separate function to loop through the array and replace duplicates. I just can't get anything to work. If someone could point me in the right direction it would be greatly appreciated.
I would maintain an array used for used numbers, and for every time you put a random number in the array, check if it is in the used array, if not then put it in both used and random.
The problem with creating a used array is that there are a limited amount of numbers to use. Sorry I should have made myself clearer. The numbers can repeat in the array just not in the same row.
Your range of items is quite limited ([1,50] it seems?), so you can easily keep an array of 50 values to keep track. Rather than actually keeping a list of all used numbers, use an array with counting variables for each number. For example:
1 2 3 4 5 6 7 8 9 10
int used[50] = {0}, myRandom; // Counters are 0 at beginning.
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < colums; ++j) {
while(1) {
myRandom = rand();
if (used[myRandom] < i) { used[myRandom]++; break; }
}
}
for (int j = 0; j < 50; ++j) used[j] = i;
}
The while loop picks a random until an unused one is found (used[myRandom < i]) and increments the value so it won't pass the check again.
The last line makes sure it can't "save" uses for later rows (i.e. no '3' in the first 5 rows, then 5 times '3' in the 6th row).
Alternatively, you could keep a row of booleans and set them to false each round. It's actually better, but I changed part of my code halfway my post and I was too lazy to adapt things.
[edit]
Make sure your number of columns doesn't get bigger than your range of possible values. Trying to pull 51 unique values out of a range of 50 is difficult. My example code above will lock into an infinite loop.