Generating random #'s

The following code is a lab that I have completed for class. I am new to C++ and am learning along the way so please be gentle if I am overlooking something that seems pretty obvious. I created a main funtion, a make board function, and a print board function for a Bingo game. Here is the code

#include <iostream>
#include <ctime>

using namespace std;

void PrintBoard(int bingo[][5])
{

for (int y = 0; y < 5; y++)
{

cout << bingo[0][y] << " " << bingo[1][y] << " " << bingo[2][y] << " " << bingo[3][y] << " " << bingo[4][y] << " " << endl;
}
return;
}

void MakeBoard(int bingo[][5])
{

for ( int y = 0; y < 5; y++)

{
bingo[0][y] = rand() % 15 + 1;
bingo[1][y] = rand() % 15 + 16;
bingo[2][y] = rand() % 15 + 31;
bingo[3][y] = rand() % 15 + 46;
bingo[4][y] = rand() % 15 + 61;

}
return;
}


int main()
{
srand((unsigned)time(0));
int bingo[5][5];

MakeBoard(bingo);
cout << "B" << " " << "I" << " " << "N" << " " << "G" << " " << "O" << endl;
PrintBoard(bingo);
cout << endl;

system("pause");
return 0;
}
The program compiles and the Bingo board is printed out 3 times as desired. The numbers on each iteration of the Bingo board are the same. How do I generate different numbers on each iteration of the Bingo board?
I don't see anything in your code that will cause the board to be printed three times.

When I do the following, I get three different boards.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{   srand((unsigned)time(0));	
    int bingo[5][5];

    for (int i=0; i<3; i++)
    {   MakeBoard(bingo);	
        cout << "B" << " " << "I" << " " << "N" << " " << "G" << " " << "O" << endl;
        PrintBoard(bingo);
        cout << endl;
    }
    system("pause");
    return 0;
}


Make sure you're not including the call to srand() inside the loop. Doing so will cause the RNG to be seeded with the same number each time srand() is called.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Considering that I wrote the exact same code in my compiler as I posted here, do you think the issue could be with my compiler itself?
Do you mean you run the program three times? time(0) returns the time in seconds so if you run the program within the same second the same number will be passed to srand, which means rand() will give you the same sequence of numbers, which means you get the same bingo board. A solution to this problem could be to wait a bit longer between running the program, or you could use a different seed that is more likely to be different each time the program runs.
Topic archived. No new replies allowed.