Hello everyone. So i'm trying to do a bingo game. I use a function to randomize my numbers inside my array and the number is between the range of the array size (zero is not included) and i have to make sure that there's no duplicate numbers inside my array because there's no duplicate number inside a bingo box. So far i just know how to check the 1d array
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int array_size = 5;
int max_num = array_size;
int value [array_size];
srand (time(NULL));
for (int i = 0; i < max_num; i++)
{
bool check;
int n;
do
{
n = rand()% max_num + 1;
check = true;
for (int x = 0; x < i;x++)
if (n == value[x])
{
check = false;
break;
}
} while (!check);
value [i] = n;
}
for (int i = 0; i < max_num; i++)
{
cout << value [i] << " ";
}
return 0;
}
Can someone help me how to check the randomize number for 2d array?
Lets say that the max_num is 1'000'000. You have already managed to get 999'999 unique numbers. How many times do you expect to have to guess before you will get that last missing value right?
That does not relate to 2d vs 1d, but it does impact the solution.
You know exactly what values you need, but not where they are. Therefore, take the desired values and shuffle them into random positions:
Here's how you can make each number unique in a 2D array, but you'll still have to use a different approach for your Bingo board. Column 1 can only have numbers 1 - 15, column 2 has 16 -30, column 3, 31 - 45, etc.
#include <ctime>
#include <iostream>
usingnamespace std;
int main()
{
constint array_size = 12; // Change to whatever size array needed
int max_num = array_size*array_size;
int numbers[array_size*array_size] = { 0 }; // Keep track of all numbers created
int value[array_size][array_size] = { { 0 }, { 0 } };
bool ok;
int n, z=0, i, j, x;
srand((unsigned)time(0));
for (i = 0; i < array_size; i++)
{
for (j = 0; j < array_size; j++)
{
do
{
ok = true;
n = 1 + rand() % max_num;
for ( x = 0; x < z; x++)
{
if (n == numbers[x])
{
ok = false;
}
}
} while (!ok);
numbers[z] = n;
value[i][j] = n;
z++;
}
}
for (i = 0; i < array_size; i++)
{
for (j = 0; j < array_size; j++)
{
if (value[i][j] < 10) // Just to make numbers into even columns
cout << " ";
if (value[i][j] < 100) // Just to make numbers into even columns
cout << " ";
cout << value[i][j] << " "; // Print numbers
}
cout << endl;
}
cout << endl << endl;
return 0;
}