Having problems with random numbers

Hey I'm working on an exercise where the program creates five different sets of lottery numbers. Each set of numbers are supposed to contain unique numbers. I am using a 2-D int array to store the numbers, and also a 2-D bool array to mark true in the location of a number if it has already been used. I'm using a while loop to check the bool array and make sure the random number hasn't already been used. Here's my problem. I can print the random numbers but they're not always unique. I have been trying to figure this out for hours. PLEASE HELP!!!!
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
//This is the lottery picker exercise from Tutorial 14
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

const int lottery = 4;
const int number = 5;
const int unique = 39;


int main() {

	int selection = 0;

	int lotteryNumbers[4][5] = {0};

	bool uniqueNumber[lottery][unique+1];

	string lotteryNames[4] =
	{"First Lottery:\t","Second Lottery:\t","Third Lottery:\t","Fourth Lottery:\t"};

	srand(time(0));

	for (int i = 0; i < lottery; i++) {

		for (int d = 0; d < unique; d++) {

			uniqueNumber[i][d] = false;

		}//end second for

		cout << "\n" << left << setw(15) << left << lotteryNames[i];

				for (int n = 0; n < number; n++) {

				do {

					selection = (1 + rand() % 39);

				}//end do
						
				while (uniqueNumber[i][selection] = false);

				uniqueNumber[i][selection] = true;
					
				lotteryNumbers[i][n] = selection;

				cout << left << setw(3) << lotteryNumbers[i][n];

			
				}//end inner for

			

		}//end outer for
	
	cout << "\n\n";

	return 0;
}//end function main 
while(uniqueNumber[i][selection] = false)
must be:
while(uniqueNubmer[i][selection] == false)
Last edited on
whenever i ran
while(uniqueNubmer[i][selection] == false)
every number in the program was the same, even in different the different sets of numbers.
now run:
while(uniqueNumber[i][selection] == true)
the first one was a syntax bug, this one is logical
Last edited on
Oh and to make sure every thing is right, move the lotteryNumbers and uniqueNumber declarations outside the main functions, that way there are always initialized to 0 so you don't have to write a loop to do the initialization.
You sir, are a genius. I tried everything I could think of to get this to work even got as far as while(uniqueNumber[i][selection] = true, but it never occurred to me that I need 'is equal to' when dealing with booleans. I knew it was something small though, it had to be. Anyway, thank you very much. Now I can go to sleep. :)
I'm glad I helped ;)
Topic archived. No new replies allowed.