Lottery Generator based on Triple X

Hello, total programming noob here. I really enjoyed making the C++ triple X game although mine was based on a plump kid trying to get into a fridge at fat camp (my failed attempts at dieting may have played a part here…). Anyway, having got to the random number generator, I thought it would be fun make a Lottery Number “predictor” (obviously it doesn’t predict anything) based on the same method.

It’s based on the UK lottery which has 6 balls numbered 1 - 59 (although for testing I’ve made it 1-6 so I can see if it’s repeating numbers). I can get 6 balls easily enough, but obviously once a ball is taken it can’t be used again. For example, if ball 1 is 11, no other ball should be 11. How do I exclude numbers that have already been generated for the previous ball? Any help is greatly appreciated. Code below:

#include <iostream>

#include <ctime>

void PrintIntroduction()

{

 //Print welcome messages to terminal

    std::cout << "\n\nHello, Welcome to Dave's Totally Accurate National Lottery Number Predictor, with is guaranteed to make you a millionaire*\n";

    std::cout << "*Guarantee not actually guaranteed, millionire chances incredibly small, you're daft as a brush is you believe this guff\n";

}

int main()

{

    PrintIntroduction();

    /* initialize random seed: */

     srand (time(NULL));

    //declare 6 balls

    const int Ball1 = rand() % 6 + 1;

    const int Ball2 = rand() % 6 + 1;

    

    if (Ball2 == Ball1);

        {

            const int Ball2 = rand() % 6 + 1;

        }

    const int Ball3 = rand() % 6 + 1;

    

    if (Ball3 == Ball1 || Ball2);

        {

            const int Ball3 = rand() % 6 + 1;

        }

 

    const int Ball4 = rand() % 6 + 1;

    if (Ball4 == Ball1 || Ball2 || Ball3);

        {

            const int Ball4 = rand() % 6 + 1;

        }

    const int Ball5 = rand() % 6 + 1;

    

    if (Ball5 == Ball1 || Ball2 || Ball3 || Ball4);

        {

            const int Ball5 = rand() % 6 + 1;

        }

    const int Ball6 = rand() % 6 + 1;

    if (Ball6 == Ball1 || Ball2 || Ball3 || Ball4 || Ball5);

        {

            const int Ball6 = rand() % 6 + 1;

        }

    std::cout << "Ball 1\n";

    std::cout << Ball1;

    std::cout << "\nBall 2\n";

    std::cout << Ball2;

    std::cout << "\nBall 3\n";

    std::cout << Ball3;

    std::cout << "\nBall 4\n";

    std::cout << Ball4;

    std::cout << "\nBall 5\n";

    std::cout << Ball5;

    std::cout << "\nBall 6\n";

    std::cout << Ball6;

    

    return 0;

}
const int Ball2 = rand() % 6 + 1;
if (Ball2 == Ball1);
{
    const int Ball2 = rand() % 6 + 1;
}

There’s two issues with this code.
The first issue is that you have a semicolon on the end of the if staterment meaning it’s equivalent to the following

const int Ball2 = rand() % 6 + 1;
if (Ball2 == Ball1)
{
    //do nothing
}
{
    const int Ball2 = rand() % 6 + 1;
}

So the if statement isn’t doing anything.

The second problem is that the Ball2 that is in the {} is in a new scope which means that it’s a new variable. Variables that are created on the stack are allocated at the beginning of their enclosing scope and destroyed at the end of it, so that new Ball2 variable is effectively created and then instantly destroyed.

Ball3 == Ball1 || Ball2

And the others(|| BallX...) are another problem. Context isn’t carried over, you aren’t comparing Ball2 to anything, this statement is exactly the same as

Ball2 || Ball 3 == Ball1

So Ball2 is going to be converted to a boolean value and all non-zero values are true. With the fact that these values are always going to be greater than one that statement is the same as

true || Ball1 == Ball3

Which is just true


To go about how you are currently trying to solve this problem would be to remove const from all of the Ball2… variables and assign them new values when they’re already used and instead of an if statment use a while loop

Thanks for your help! I’ll give that a crack.

Hi,

I finally found the time to return to this, I’ve ended up with the following (include/intro is the same as my first post so has been cut for the sake of brevity)

int main()
{
    PrintIntroduction();

    
       /* initialize random seed: */
     srand (time(NULL));

    //declare 6 balls

    int Ball1 = rand() % 6 + 1;
    int Ball2 = rand() % 6 + 1;
    int Ball3 = rand() % 6 + 1;
    int Ball4 = rand() % 6 + 1;
    int Ball5 = rand() % 6 + 1;
    int Ball6 = rand() % 6 + 1;

     while (Ball2 == Ball1)
    {
        Ball2 = rand() % 6 + 1;
    }
    
     while (Ball3 == Ball1 || Ball3 == Ball2)
    {
        Ball3 = rand() % 6 + 1;
    }

    while (Ball4 == Ball1 || Ball4 == Ball2 || Ball4 == Ball3)
    {
        Ball4 = rand() % 6 + 1;
    }

    while (Ball5 == Ball1 || Ball5 == Ball2 || Ball5 == Ball3 || Ball5 == Ball4)
    {
        Ball5 = rand() % 6 + 1;
    }
    
     while (Ball6 == Ball1 || Ball6 == Ball2 || Ball6 == Ball3 || Ball6 == Ball4 || Ball6 == Ball5)
    {
        Ball6 = rand() % 6 + 1;
    }

    std::cout << "Ball 1\n";
    std::cout << Ball1;
    std::cout << "\nBall 2\n";
    std::cout << Ball2;
    std::cout << "\nBall 3\n";
    std::cout << Ball3;
    std::cout << "\nBall 4\n";
    std::cout << Ball4;
    std::cout << "\nBall 5\n";
    std::cout << Ball5;
    std::cout << "\nBall 6\n";
    std::cout << Ball6;

    
    
    return 0;
}

This seems to give a decent impression of non repeating random results. Are there any obvious issues and is there an more elegant way of coding the “while” statements? Thanks in advance!

Well there are more elegant solutions they all require a bit more advanced C++. So what you have right now is perfectly fine for what you have learnt so far.

Great, thanks again for your help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms