Mega milions

Hi there;
I want to make program that show me ,how long I need to wait for my lucky day.
I want to select only 1 combination ,let say numbers 15,25,35,45,55 .
Program will compare my numbers with random numbers , if find it program stop.
I assume draw is 2 x per week ,if year has 52 weeks x 2 there is 104 draws per year .

I made this code ,but it does not work ,I don't know how to make it .
Thanks

int n[5]; // num combination;
int a, b, c, d, e;
int year = 104;
int num = 1;
int w = 1;

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
while (num !=2 )
	{
		for(int j = 1;j  < 10;j++)
		{
			cout << "\n In year ( " << w++ << " ) was selected this numbers  -->  " << endl << endl;

			for (int i = 1; i < year; i++)
			{
				a = n[0] + (rand() % 70);
				b = n[1] + (rand() % 70);
				c = n[2] + (rand() % 70);
				d = n[3] + (rand() % 70);
				e = n[4] + (rand() % 70);
			
				//if(a == 10 || b ==10 || ... ) //it does not work :/

				cout << " Num's in draw  " << i + 1 << "  are  :    ";
			
				cout << a << " " << b << " " << c << " "
					 << d << " " << e << " " << endl;
			}
			cout << endl;
		}
		num++;
	}
Last edited on
Something like this. Be prepared to run this for a very (very) long time.

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

constexpr std::size_t N = 5 ; // 5 numbers
constexpr int MAX_NUMBER = 100 ; // in the interval [ 1, 100 ]

using numbers = std::array< int, N > ;

numbers random() // generate random numbers
{
    static std::mt19937 rng( std::random_device{}() ) ;
    static std::uniform_int_distribution<int> distrib( 1, MAX_NUMBER ) ;

    numbers n ;
    for( int& v : n ) v = distrib(rng) ;
    return n ;
}

numbers guess() // user's guess for the numbers
{
    std::cout << "enter " << N << " guesses\n" ;

    numbers n ;
    for( int& v : n )
    {
        std::cout << "? " ;
        if( !(std::cin >> v) )
        {
            // invalid input. inform the user, and throw the bad input away
            std::cout << "please enter a number\n" ;
            std::cin.clear() ;
            std::cin.ignore( 1'000'000, '\n' ) ;
        }
    }

    return n ;
}

bool matched( const numbers& a, const numbers& b ) // true if numbers a and b are identical
{
    return std::equal( a.begin(), a.end(), b.begin() ) ;
}

int main()
{
    const auto generated_numbers = random() ;

    unsigned long long num_guesses = 1 ;
    const int guesses_per_week = 2 ;

    while( !matched( generated_numbers, guess() ) )
    {
        std::cout << "not matched. try again\n" ;
        ++num_guesses ;
    }

    std::cout << "it took you " << num_guesses / guesses_per_week << " weeks ("
              << double(num_guesses) / 104 << " years) to make the right guess\n" ;
}
For a Mega Millions ticket you choose 5 numbers from 1 to 70, without duplicates.
There is also a separate Multiplier number from 1 to 25.

70 choose 5 is 12103014.
So on average it takes that many tickets to win.
With 104 tickets a year that's 12103014 / 104, about 116375 years.
The program should only take a few seconds, though.

But to win the biggest prize you have to hit the multiplier, too.
So that's 12103014 * 25 = 302575350.
302575350 / 104 is about 2909378 years.
The program should take on average 25 times longer than before.
Last edited on
Without duplicates using a set, 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
#include <iostream>
#include <random>
#include <set>
#include <algorithm>

constexpr size_t N {5}; // 5 numbers
constexpr int HIGH_NUMBER {70};	// Highest number

using numbers = std::set<size_t>;

numbers random() // generate random numbers
{
	static std::mt19937 rng(std::random_device {}());
	static std::uniform_int_distribution<size_t> distrib(1, HIGH_NUMBER);

	numbers n;

	for (; n.size() != N; n.insert(distrib(rng)));

	return n;
}

numbers guess() // user's guess for the numbers
{
	std::cout << "Enter " << N << " guesses: ";

	numbers n;

	for (size_t v {}; n.size() != N; ) {
		if (!(std::cin >> v)) {
			std::cout << "Please enter a number\n";
			std::cin.clear();
			std::cin.ignore(1'000'000, '\n');
		} else
			if (!n.insert(v).second)
				std::cout << "Duplicate number\n";
	}

	return n;
}

bool matched(const numbers& a, const numbers& b)
{
	return std::equal(a.begin(), a.end(), b.begin());
}

int main()
{
	constexpr int guesses_per_week {2};
	const auto genNum {random()};

	unsigned long long guesses {};

	for (; !matched(genNum, guess()); ++guesses)
		std::cout << "not matched. try again\n";

	std::cout << "it took you " << guesses / guesses_per_week << " weeks ("
		<< double(guesses) / 104 << " years) to make the right guess\n";
}

You've misunderstood the question. He wants to simulate playing the same ticket (e.g., 15,25,35,45,55) for every draw of the Mega Millions Lottery. Supposing 2 draws per week, how many years will he have to wait before he hits the jackpot.

Also, he seems to have forgotten/misunderstood that the biggest jackpot requires not only hitting the main five numbers but also hitting another separate number from 1 to 25.

The answer is on average he will wait 116375 years to hit the main five numbers and 2909378 years to hit the main five numbers and the bonus (so-called "multiplier") number.
Last edited on
Topic archived. No new replies allowed.