Help Generating Unique Numbers

I need help creating Unique numbers in my random number generator. Each line of ticket should only display the a number once. Can anyone give me a hint as to how I can do this? I have only learned about functions and array's so I can't use anything beyond that.

I would like to apologize for the messy code since this is my first programming course.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

#define myRand(a,b) (a+rand() % (b-a+1))
#define die(msg) {cerr << msg <<endl; exit(1);}

using namespace std;

int lotto(int);

int main()
{
    int num;
    cout <<"Mega Million ";
    cin >> num;

    lotto(num);
    return(0);
}

int lotto(int n)
{
    int array[5];
    char m = 'A';

    srand(time(NULL));
    for(int i = 0; i < n; i++)
    {
        if (i % 10 == 0)
            cout <<"\n     Mega Million    Mega\n" << endl;
        if (m > 'J') m = 'A';
            cout << m++ << ".  ";
        for(int j = 0; j < 5; j++)
        {
            array[j] = myRand(1,56);
        }

        void bubble_sort(int x[], int);
        bubble_sort(array,5);

        for(int l = 0; l < 5; l++)
        {
            string lottoNum;
            lottoNum = (array[l] < 10)?"0":"";
            cout << lottoNum << array[l] << " ";
        }
        int mega(int);
        mega(i);
        cout<<endl;
    }
    cout<<endl;
}

int mega(int l)
{
    int c;
    string megaNum;
    c = myRand(1,46);
    megaNum = (c < 10)?"   0":"   ";
    cout << megaNum << c;
}

void bubble_sort(int x[], int n)
{
    void swap(int x[], int i, int j);
    for(int i = 0; i < n; i++)
    {
        for(int j = i+1; j < n; j++)
        {
            if (x[i] > x[j])
                swap(x, i, j);
        }
    }
}

void swap(int x[], int i, int j)
{
   int t;
   t = x[i];
   x[i] = x[j];
   x[j] = t;
}


Last edited on
I need help creating Unique numbers in my random number generator. Each line of ticket should only display the a number once. Can anyone give me a hint as to how I can do this? I have only learned about functions and array's so I can't use anything behind that.


Create an array that holds all values a ball might represent (1-56 for example.) Shuffle the array. Treat the first five values as the five balls that are picked.
Cire,

Can I create that as another function or should it go inside my lotto function?
It can be done either way. I would probably put it in its own function which might look similar to:

1
2
3
4
5
6
7
8
9
10
11
12
13
void do_picks(int* picks) // or void do_picks(int picks[]) is equivalent
{
    // set up the ball pool
    int ball_pool[ball_pool_size];
    // ...

    // shuffle it
    // ...

    // pick 'em
    for (unsigned i = 0; i < num_picks; ++i)
        picks[i] = ball_pool[i];
}
Thanks Cire. I will give it a try!
Topic archived. No new replies allowed.