Almost done random number generator. Help please

[EDIT] The OP post has been deleted. Quoted here:
olorin82 wrote:
Hi I can make the program generate random numbers but still it should the following:

Create a lotto number generator:

Constructor takes two parameters that determine the range of numbers that the generator produces.

Write a program that generates numbers for three different games:

1. Lotto A: 7 numbers 1 – 40
2. Lotto B: 6 numbers 1 – 48
3. Lotto C: 5 numbers 1 – 50

Use either generate or generate_n algorithm to generate the three sets of numbers.

Print each set of numbers using a suitable algorithm and output stream iterator.
Then find numbers that appear in all three sets and print them. Use set_intersection algorithm to find the numbers that appear in all three sets:

• Find matching numbers from two sets and print them using for_each algorithm
• Find matching number from the third set and print them using for_each algorithm

To print the numbers with index numbering you need to use a function object (can also be implemented with a lambda) to remember the index between calls.

Example:
Lotto A: 1 12 24 36 11 15 32
Lotto B: 24 7 11 18 35 1

Matching numbers:

#1: 1
#2: 11
#3: 24
Lotto C: 47 1 40 24 4

Matching numbers in three sets:

#1: 1
#2: 24

After finding the matching numbers ask user if he/she wants to continue. If the answer is yes then generate another three sets of lotto numbers.


MY CODE: sorry the format code button is not working for me

#include <algorithm>
#include <iostream>
#include <random>
#include <vector>


int randGen_1() {
return rand() % 10 + 1;
}

class RandGen_2 {
public:
RandGen_2() : numbers() { srand(time(NULL)); }
int operator()();

private:
vector<int> numbers;
};

std::vector<int> generate_lotto(std::size_t num, int low, int high)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(low, high);

std::vector<int> lotto(num);
std::generate(lotto.begin(), lotto.end(), [&]() { return distrib(gen); });

return lotto;
}

int main()
{
std::vector<int> lotto_a = generate_lotto(7, 1, 40);
std::vector<int> lotto_b = generate_lotto(6, 1, 48);
std::vector<int> lotto_c = generate_lotto(5, 1, 50);
}

void main(void) {
vector<int> numbers(10);

srand(time(NULL));
generate(numbers.begin(), numbers.end(), randGen_1); */

RandGen_2 randGen_2;
generate(numbers.begin(), numbers.end(), randGen_2);
for (auto n : numbers)
cout << n << endl;
}

int RandGen_2::operator()() {
int number;

do {
number = rand() % 10 + 1;
} while (find(numbers.begin(), numbers.end(), number) != numbers.end());
numbers.push_back(number);
return number;
}

[/EDIT]

1
2
3
4
5
6
7
8
int main()
{
  // code
}

void main(void) {
  // code
}

That makes no sense. There can be only one "main" in a program.
The "main" must return int. (Preferably 0 or small, positive value.)
A function that does not take any arguments does not need (void). The () is fine.

Use either generate or generate_n algorithm to generate the three sets of numbers.

"Lotto" does often mean a game, where there is a pool of uniquely numbered balls and you draw some balls. Say 7 from 40. Since each ball has unique number, no number can be drawn more than once.

Use of std::generate/std::generate_n simply with random number generator can yield same value more than once. I would use std::iota and std::shuffle for "lotto".

You can type the tags to post if the button does not "work".
Last edited on
To generate the required 3 lotto vectors, consider:

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
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
#include <numeric>

class Lotto {
public:
	Lotto(std::size_t num, size_t high) {
		static auto Rand {std::mt19937 {std::random_device {}()}};
		std::vector<size_t> allnums(high);

		std::iota(allnums.begin(), allnums.end(), 1);
		std::shuffle(allnums.begin(), allnums.begin() + high, Rand);
		std::copy_n(allnums.begin(), num, std::back_inserter(nums));
	}

	friend std::ostream& operator<<(std::ostream& os, const Lotto& lot);
private:
	std::vector<size_t> nums;
};

std::ostream& operator<<(std::ostream& os, const Lotto& lot)
{
	std::copy(lot.nums.begin(), lot.nums.end(), std::ostream_iterator<size_t>(os, " "));
	return os;
}

int main()
{
	const Lotto lotto_a(7, 40);
	const Lotto lotto_b(6, 48);
	const Lotto lotto_c(5, 50);

	std::cout << "lotto a\n" << lotto_a << "\n\n";
	std::cout << "lotto b\n" << lotto_b << "\n\n";
	std::cout << "lotto c\n" << lotto_c << '\n';
}



lotto a
2 8 16 24 40 37 23

lotto b
10 19 23 17 42 13

lotto c
20 8 16 44 23

homework instructions wrote:
Use set_intersection algorithm to find the numbers that appear in all three sets

http://www.cplusplus.com/reference/algorithm/set_intersection/
Note that set_intersection() requires the specified containers to be already sorted. The numbers generated by Lotto are not sorted. So they will need to be.

Consider something like:

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
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
#include <numeric>
#include <iterator>

class Lotto {
public:
	Lotto(std::size_t num, size_t high) {
		static auto Rand {std::mt19937 {std::random_device {}()}};
		std::vector<size_t> allnums(high);

		std::iota(allnums.begin(), allnums.end(), 1);
		std::shuffle(allnums.begin(), allnums.end(), Rand);
		std::copy_n(allnums.cbegin(), num, std::back_inserter(nums));
		std::sort(nums.begin(), nums.end());
	}

	Lotto operator&(const Lotto& lo) const {
		std::vector<size_t> inter;

		std::set_intersection(lo.nums.cbegin(), lo.nums.cend(), nums.cbegin(), nums.cend(), std::back_inserter(inter));
		return inter;
	}

	friend std::ostream& operator<<(std::ostream& os, const Lotto& lot);

private:
	Lotto(const std::vector<size_t>& n) : nums(n) {}

	std::vector<size_t> nums;
};

std::ostream& operator<<(std::ostream& os, const Lotto& lot)
{
	size_t cnt {1};

	for_each(lot.nums.begin(), lot.nums.end(), [&](auto i) {os << '#' << cnt << ": " << i << '\n'; ++cnt; });
	return os;
}

int main()
{
	const Lotto lotto_a(7, 40);
	const Lotto lotto_b(6, 48);
	const Lotto lotto_c(5, 50);
	const auto lotto_ab {lotto_a & lotto_b};
	const auto lotto_ac {lotto_a & lotto_c};
	const auto lotto_bc {lotto_b & lotto_c};
	const auto lotto_abc {lotto_a & lotto_b & lotto_c};
	// or const auto lotto_abc {lotto_ab & lotto_c};

	std::cout << "lotto a\n" << lotto_a << "\n";
	std::cout << "lotto b\n" << lotto_b << "\n";
	std::cout << "lotto c\n" << lotto_c << "\n";
	std::cout << "lotto_ab\n" << lotto_ab << "\n";
	std::cout << "lotto_ac\n" << lotto_ac << "\n";
	std::cout << "lotto_bc\n" << lotto_bc << "\n";
	std::cout << "lotto_abc\n" << lotto_abc << "\n";
}


which one run displays:


lotto a
#1: 2
#2: 9
#3: 10
#4: 11
#5: 19
#6: 20
#7: 40

lotto b
#1: 2
#2: 3
#3: 17
#4: 22
#5: 41
#6: 48

lotto c
#1: 2
#2: 3
#3: 15
#4: 37
#5: 39

lotto_ab
#1: 2

lotto_ac
#1: 2

lotto_bc
#1: 2
#2: 3

lotto_abc
#1: 2


Last edited on

The main lotto here has six numbers, 1 to 59, each ticket has six numbers also.
(There are variations, Euromillions, thunderball . . . I never do it these days anyway).
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
#include<iostream>
#include<vector>
#include <time.h> 
#include <iomanip>

using namespace std;

int range(int f,int l)
   { return (rand() % ((l-f)+1)) + f;}
 
   
 vector<int>shuffle(vector<int>a)
 {
  int r=a.size()-1;
    for(int n=0;n<a.size()-2;n++)
    swap (a[n], a[range(n,r)]);
return a;
}
   
   int find(vector<int> v,int num)
   {
   	for (int i=0;i<v.size();i++) 	if (v[i]==num) return num;
   	return 0;	
   }
   
   vector<int> generate(int num,int start,int finish)
   {
   	 vector<int> fixed;
   	 int r=(finish-start)+1;
   	fixed.resize(r);
   	for (int i=start;i<=finish;i++) fixed[i-start]=i;
   	vector<int> v;
   	v.resize(r);
   	v=shuffle(fixed);
   	v.resize(num);
   	return v;
   }
   
   vector<int> results(vector<int> winners,vector<int>mine)
   {
   	vector<int> ret;
   	int counter=0,f=0;
   	for (int i=0;i<winners.size();i++)
   	{
   		f=find(winners,mine[i]);
   		if (f != 0) 
   		{
   		counter+=1;
   		ret.resize(counter);
   		ret[counter-1]=f;	   	
   		}
   	}
   	return ret;
   }
   
   void print(vector<int> v)
   {
   	for (int i=0;i<v.size();i++) cout<<setw(2)<<v[i]<<"  ";
   	cout<<endl;
   }
   
   int main()
   {
   	// lotto number range
   	int start=1;
   	int end  =59;
   	int number=6;
   	
   	 srand (time(NULL));// randomize
   	 
   	 for(int k=0;k<=10;k++) // 10 runs
   	 {
   	vector<int> winners = generate(number,start,end);
   	cout<<"Winning numbers       ";
   	print(winners);
   	vector<int>mine=generate(number,start,end);
   	cout<<"My numbers            ";
   	print(mine);
   	vector<int> mywinners=results(winners,mine);
   	cout<<"My winning numbers    ";
   	if (mywinners.size() != 0) print(mywinners);
   	else
   	cout<<"      NONE!  "<<endl;
   	cout<<endl<<endl;
   }
   
   cout<<endl;
  
	 cout <<"Press return to end . . ."<<endl; 
cin.get();	 
   }  

Maybe I'll start doing it again.

@oggin. This helps the OP how? There's issues with this code. If you want to do something like this, consider:

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
#include <iostream>
#include <vector>
#include <iomanip>
#include <random>
#include <algorithm>
#include <numeric>

std::vector<int> generate(int num, int finish)
{
	static auto Rand {std::mt19937 {std::random_device {}()}};
	std::vector<int> allnums(finish);
	std::vector<int> nums;

	std::iota(allnums.begin(), allnums.end(), 1);
	std::shuffle(allnums.begin(), allnums.end(), Rand);
	std::copy_n(allnums.cbegin(), num, std::back_inserter(nums));
	std::sort(nums.begin(), nums.end());
	return nums;
}

std::vector<int> results(const std::vector<int>& winners, const std::vector<int>& mine)
{
	std::vector<int> ret;

	for (int m : mine)
		if (std::find(winners.cbegin(), winners.cend(), m) != winners.cend())
			ret.push_back(m);

	return ret;
}

void print(const std::vector<int>& v)
{
	for (int i : v)
		std::cout << std::setw(2) << i << "  ";

	std::cout << '\n';
}

int main()
{
	const int end {59};
	const int number {6};
	const int runs {10};

	for (int k = 0; k < runs; k++) {
		const auto mine {generate(number, end)};
		const auto winners {generate(number, end)};
		const auto mywinners {results(winners, mine)};

		std::cout << "Winning numbers       ";
		print(winners);

		std::cout << "My numbers            ";
		print(mine);

		std::cout << "My winning numbers    ";

		if (!mywinners.empty())
			print(mywinners);
		else
			std::cout << "      NONE!  \n";

		std::cout << "\n\n";
	}
}



Winning numbers       17  21  23  37  38  59
My numbers            14  18  19  43  51  57
My winning numbers          NONE!


Winning numbers        4  28  37  46  47  51
My numbers             2  13  21  24  33  49
My winning numbers          NONE!


Winning numbers       15  35  51  54  56  57
My numbers            10  19  27  39  52  59
My winning numbers          NONE!


Winning numbers        4   9  21  33  52  57
My numbers             3  37  47  49  55  57
My winning numbers    57


Winning numbers        7  18  22  25  29  39
My numbers             8  24  38  39  44  58
My winning numbers    39


Winning numbers        2  16  21  23  35  48
My numbers            19  20  31  50  55  58
My winning numbers          NONE!


Winning numbers       19  23  24  30  36  58
My numbers             9  14  17  21  49  52
My winning numbers          NONE!


Winning numbers       17  32  33  46  48  59
My numbers             4   9  18  41  57  59
My winning numbers    59


Winning numbers        6  10  13  21  24  46
My numbers             6  21  26  32  38  40
My winning numbers     6  21


Winning numbers       25  33  34  38  42  44
My numbers             3  16  19  25  35  56
My winning numbers    25

Last edited on
If std::set can be used instead of std::vector, then generate can be somewhat simplified:

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
#include <algorithm>
#include <iostream>
#include <random>
#include <set>
#include <iterator>

class Lotto {
public:
	Lotto(std::size_t num, size_t high) {
		static auto Rand {std::mt19937 {std::random_device {}()}};
		auto randNo {std::uniform_int_distribution<size_t> {1, high}};

		for (; nums.size() != num; nums.insert(randNo(Rand)));
	}

	Lotto operator&(const Lotto& lo) const {
		std::set<size_t> inter;

		std::set_intersection(lo.nums.cbegin(), lo.nums.cend(), nums.cbegin(), nums.cend(), std::inserter(inter, inter.begin()));
		return inter;
	}

	friend std::ostream& operator<<(std::ostream& os, const Lotto& lot);

private:
	Lotto(const std::set<size_t>& n) : nums(n) {}

	std::set<size_t> nums;
};

std::ostream& operator<<(std::ostream& os, const Lotto& lot)
{
	size_t cnt {1};

	for_each(lot.nums.cbegin(), lot.nums.cend(), [&](auto i) {os << '#' << cnt << ": " << i << '\n'; ++cnt; });
	return os;
}

int main()
{
	const Lotto lotto_a(7, 40);
	const Lotto lotto_b(6, 48);
	const Lotto lotto_c(5, 50);
	const auto lotto_ab {lotto_a & lotto_b};
	const auto lotto_ac {lotto_a & lotto_c};
	const auto lotto_bc {lotto_b & lotto_c};
	const auto lotto_abc {lotto_a & lotto_b & lotto_c};
	// or const auto lotto_abc {lotto_ab & lotto_c};

	std::cout << "lotto a\n" << lotto_a << "\n";
	std::cout << "lotto b\n" << lotto_b << "\n";
	std::cout << "lotto c\n" << lotto_c << "\n";
	std::cout << "lotto_ab\n" << lotto_ab << "\n";
	std::cout << "lotto_ac\n" << lotto_ac << "\n";
	std::cout << "lotto_bc\n" << lotto_bc << "\n";
	std::cout << "lotto_abc\n" << lotto_abc << "\n";
}

Last edited on

Seeplus, your code runs well on my 64 bit g++ --version 8.1.0 (mingw)
Dev-C++, even a recent version, seems to have an older g++ (4.9.2), and I get errors.
Here is my own method adjusted a bit to suit the OP's request.
My method just slogs it out, it doesn't use the more subtle C++ routines.

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
#include<iostream>
#include<vector>
#include <iomanip>   
#include <time.h> 
#include<conio.h>
using namespace std;

int range(int f,int l)
   { return (rand() % ((l-f)+1)) + f;}
  
 vector<int>shuffle(vector<int>a)
 {
  int r=a.size()-1;
    for(int n=0;n<a.size()-2;n++)
    swap (a[n], a[range(n,r)]);
return a;
}

   int find(vector<int> v,int num,int &val)
   {
   	for (int i=0;i<v.size();i++) if (v[i]==num){val=v[i]; return 1;};
   	return 0;	
   }
   
   vector<int> generate(int num,int start,int finish)
   {
   	 vector<int> fixed,v;
   	 int r=(finish-start)+1;
   	fixed.resize(r);
   	for (int i=start;i<=finish;i++) fixed[i-start]=i;
   	v.resize(r);
   	v=shuffle(fixed);
   	v.resize(num);
   	return v;
   }
   
   vector<int> results(vector<int> v1,vector<int>v2)
   {
   	vector<int> ret;
   	int counter=0,f=0,val=0;
   	if (v2.size()<v1.size()) swap (v2,v1);
   	for (int i=0;i<v1.size();i++)
   	{
   		f=find(v2,v1[i],val);
   		if (f)
   		{
   		counter+=1;
   		ret.resize(counter);
   		ret[counter-1]=val;	   	
   		}
   	}
   	return ret;
   }
   
   void print(vector<int> v,int flag=0)
   {
   if(v.size()==0) cout<<"NONE"<<endl;
   if(flag==0)	for (int i=0;i<v.size();i++) cout<<setw(2)<<v[i]<<"  ";
   if(flag==1)  for (int i;i<v.size();i++) cout<<"#"<<i+1<<"  "<<v[i]<<endl;
   	cout<<endl;
   }
    
   int main()
   {
   srand (time(NULL));
   int counter=0;
   vector<int> a,b,c,e;
   string answer="y";
   	 
   	 while (answer=="y")
   	 {
   	 	cout<<endl;
   	 	counter+=1;
   	 	if(counter!=1)
   	 	{
   	 cout<<"Do you want more?  key y or n?"<<endl;
   	 answer=getch();
       }
   	 if(answer != "y") break;
   	 cout<<endl<<endl;
   	 cout<<"  ----------------  "<<endl;
   	  if(counter==1) a ={1,12,24,36,11,15,32}; else a =generate(7,1,40);
   	  if(counter==1) b ={24,7,11,18,35,1};     else b =generate(6,1,48);
   	  if(counter==1) c ={ 47,1,40,24,4};       else c =generate(5,1,50);
   	  if(counter==1) cout<<"original set"<<endl;
   	 cout<<"A = ";print(a);
   	 cout<<"B = ";print(b);
   	 cout<<endl;
   	 vector<int> d=results(a,b);
   	 cout<<"Matching numbers:"<<endl;
   	 print(d,1);
   	 cout<<endl;
   	 cout<<"C = ";print(c);
	 e=results(d,c);
		cout<<endl;
		cout<<"Matching numbers in three sets:"<<endl;
   	 print(e,1);	
   	 }	 
   }   





Topic archived. No new replies allowed.