fill array with random numbers without repetition in the column

Hello, I am generating bingo cards for each player.
The bingo card is represented by an array and has "BINGO"
written across the top row of this array.
I used a 4-dimensional array called
darray[numOfPlayers][numCardsPerPlayer][6][5]
with the 6 being the number of rows and 5 being the number of columns.

My problem with this code is that the bingo card cannot have repeating numbers in the same column. I have created a do-while loop that checks the entire column for any repeats but it does not work. Any help with this loop would be appreciated.

The Do-While loop begins on line 58!
Sorry if there is too much code for the question, I figured it would be necessary in case anyone wanted to modify it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void){

   srand((int)time(0));
   int numOfPlayers = 0;
   int numCardsPerPlayer = 0;
   int numOfRows = 6;
   int numOfCols = 5;                           
   
   printf("Enter the number of players: ");
   scanf("%d", &numOfPlayers);
   printf("\nEnter the number of BINGO cards per player: ");
   scanf("%d", &numCardsPerPlayer);
   printf("\nYou are playing a game with %d players and each player will have %d cards\n", numOfPlayers, numCardsPerPlayer);
   
   char darray[numOfPlayers][numCardsPerPlayer][6][5];
   
   int i;
   int j;
   int k;
   int l;
   
   /*Fills 4d loop with BINGO on top row and zeros everywhere else*/
   for(i=0; i< numOfPlayers; i++){
    	for(j=0; j< numCardsPerPlayer; j++){
   	    	for(k=0; k < numOfRows; k++){
   		    	for(l=0; l<numOfCols; l++){   			
			   	darray[i][j][k][l] = '0';			   	
   			   	if(k == 0 ){                           // Fills top row of bingo card with BINGO
   			   	if(l == 0){	darray[i][j][k][l]='B';}
   			   	if(l == 1){	darray[i][j][k][l]='I';}
   			   	if(l == 2){	darray[i][j][k][l]='N';}
   			   	if(l == 3){	darray[i][j][k][l]='G';}
   			   	if(l == 4){	darray[i][j][k][l]='O';}
			       }			   
				   }
		   }
		   
	   }
   }
   
   /* Generates bingo card for every user */
   //(rand()%15) + 1   
   bool isRepeated;
   int temp;
   char columnList[5];
   int m;
   
   for(i =0; i < numOfPlayers; i++){
    	for(j=0; j < numCardsPerPlayer; j++){
   	    	for(k=1; k < numOfRows; k++){
   	    		for(l=0; l < numOfCols; l++){
   	    			// FIXME: GET COLUMNS TO NOT REPEAT
							do{
					            isRepeated = true;
				            	temp = 1+(l*15) + rand()%15;
											
					            for (m = 0; m < 5; m++){
						            if (temp == darray[i][j][k][m]){
							        isRepeated = false; // Same number found, so create a new number
							            }
					                }   
				            }
							while (!isRepeated);
                			darray[i][j][k][l] = temp;
				   }
		   }
	   }
   
   
   /* prints our every bingo card for every player */
   
   for( i=0;i<numOfPlayers;i++){
   	
    printf("\tPlayer %d\n",i+1); // Prints the player number above their cards
    
    	for(int j=0; j < numCardsPerPlayer;j++){
   	    	for(int k = 0; k < numOfRows;k++){
   			    for(int l=0; l<numOfCols;l++){
   			    
				   if(k == 0){printf("%4c", darray[i][j][k][l]);}
				   if(k != 0){printf("%4d", darray[i][j][k][l]);}	
   		        
   		        
   	            }
   	            
   	       	   printf("\n");
            }
            printf("\n");
	    }
	}
   

	return 0;
}
}
Last edited on
This is some awkward C code. You'd have been better off with C++.

Anyway, in what way does it not work? Does it always repeat? Does it sometimes repeat? Does it crash?
Think of lottery machine. It has balls. Each ball has a unique number. The balls are shuffled before you start picking them up. You won't get same number twice.

Same with deck of cards. Shuffled. You cannot draw same card twice.

Shuffling creates the randomness. See http://www.cplusplus.com/reference/algorithm/shuffle/
Hello, the code above generates *almost* random numbers. The numbers in the column repeat every couple of other cards. I am trying to have the numbers to never repeat in a column.

Regarding the shuffle, I don't think that is the direction I am supposed to go in. I am supposed to generate a new random number every time i fill the array and keep generating numbers if the number has been repeated in the specific column.

***************************************************************
I have figure it out. I was iterating through the rows of darray when I should have been iterating through the columns. I switched k to m and m to l. Also, I had a "}" somewhere where it should not have been. Note that the code below is nested withing four "for" loops i,j,k,l with l being the innermost loop
here is the do-while loop that worked:

1
2
3
4
5
6
7
8
9
10
do{
			isRepeated = true;
	            	temp = 1+(l*15) + rand()%15;
											
			            for (m = 0; m < 5; m++){
				            if (temp == darray[i][j][m][l]){
					        isRepeated = false; // Same number found, so create new number
			        }}}
							while (!isRepeated);
                			darray[i][j][k][l] = temp;


Last edited on
Regarding the shuffle, I don't think that is the direction I am supposed to go in. I am supposed to generate a new random number every time i fill the array

Think of this (case worse than yours):
1. The valid values are 1..1'000'000
2. You need 1'000'000 unique random numbers

Following your "supposed" algorithm, the beginning of the array will seem easy. However, to get the last "random" number, you would have to repeat the 1 + rand() % 1'000'000 until you do get the one and only missing unique value. It can take more than 1'000'000 tries.

The shuffling calls rand() exactly N-1 times. 999'999 calls for the million case to get all random numbers and not a single repeat required.


Your valid values seem to be 0..14 (+ 1 + col*15).
You need only 5 values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <utility> // std::swap
#include <cstdlib>
#include <ctime>

int main() {
  const int N = 15;
  int pool[N];
  srand( time(0) );
  for ( int col = 0; col < 4; ++col ) {
    // reset pool
    for ( int ind=0; ind < N; ++ind ) {
      pool[ ind ] = ind;
    }
    // shuffle
    for ( int i= N-1; i>0; --i ) {
      int r = rand() % (i+1);
      std::swap( pool[ i ], pool[ r ] );
    }
    // show
    std::cout << "L=" << col << ": ";
    for ( int num=0; num < 5; ++num ) {
      std::cout << ' ' << pool[ num ] + N * col + 1;
    }
    std::cout << '\n';
  }
  return 0;
}

L=0:  8 10 7 13 4
L=1:  23 22 16 18 30
L=2:  39 41 42 35 36
L=3:  48 56 59 47 58

Are they "random", are they unique?
Last edited on
Topic archived. No new replies allowed.