pseudo rand exercise help!

For some reason I am totally lost, i literally understand everything until the part where "balls[i] == ball[j]", basically I dont want the same numbers repeated in the lottery........ but if ball[i] is assigned a random number and ball[j] isnt, how are they being compared!?

idk why something so simple is confusing me!!!


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
void lottery(int,int);
int main()
{

    lottery(49,6);

    return 0;
}
void lottery(int total_balls,int balls_to_draw)
{
    if(total_balls < balls_to_draw)
        return;

    srand(time(NULL));

    int *balls = new int[balls_to_draw];

    for(int i = 0; i < balls_to_draw;i++)
    {
        balls[i] = rand() % total_balls + 1;

        for(int j = 0; j < i + 1; j++)
        {  
            if(balls[i] == balls[j] && i != j)
            {
                i--;
                break;
            }
            else if(j == i)

                cout << balls[j] << endl;
        }
    }
 delete[] balls;
}
Whew that's some interesting code, was that written by a professor or something? Here's a better example of the same principle, but ... professional

(don't forget to #include <vector> )

1
2
3
4
5
6
7
8
9
10
void lottery(int ballQuantity, int drawQuantity) {
    std::vector<int> balls;
    for(int ball = 0; ball < ballQuantity; ball++) balls.push_back(ball);
    
    for(int draw = 0; draw < drawQuantity && balls.size() > 0; draw++) {
        int pick = rand() % balls.size();
        std::cout << balls[pick] << "\n";
        balls.erase(balls.begin() + pick);
    }
}


maybe some comments can help you understand that spaghetti:

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
void lottery(int total_balls,int balls_to_draw)
{
    // can't draw more balls then you have in the pool of balls to draw
    if(total_balls < balls_to_draw)
        return;

    // initialize your pseudo random generator to the current seconds, if you run this function twice in the same second you'll get the same results!
    // you are less likely to run into problems if you srand a single time (maybe in your main or a startup procedure)
    srand(time(NULL));


    // create a new array of balls... This is just incorrect and a poor display- use int balls[balls_to_draw]; instead of creating a new array...
    int *balls = new int[balls_to_draw];

    for(int i = 0; i < balls_to_draw;i++)
    {
        // request a random number between 0 - 48 in this example, you should be explicit with your order of operations here, it will work but parens would make intent obvious
        balls[i] = rand() % total_balls + 1;

        // we are still inside the other loop, but starting a second loop from 0 to the currently pulled ball so we can check if we have ever pulled that ball before, this is a super hacky way
        // to make sure we don't pull the same ball again. i--; break; basically says redo this current outside loop because we already have that ball in the list. yuck.
        for(int j = 0; j < i + 1; j++)
        {  
            // do we already have this ball in the list?
            if(balls[i] == balls[j] && i != j)
            {
                i--;
                break;
            }
            // we don't already have this ball and have reached the current ball, yay?
            else if(j == i)
                cout << balls[j] << endl;
        }
    }
    // seriously don't do this. this is just bad practice, change this line if you change the int *balls = new ..... to balls[balls_to_draw];
 delete[] balls;
}

thank you so MUCH FOR YOUR BREAK DOWN AND YOUR TIME. I guess I just need more practice for it to click to me. I'm going to do a review of some of the basics, maybe thats why i'm a little confused.

But thank you again...... I guess practice makes perfect.
Topic archived. No new replies allowed.