user3859339
user3859339

Reputation: 11

No Double Numbers

I'm making a powerball lottery where the numbers will be bubble sorted in ascending order. The quick picks (QP) will be between 1-56, and the powerball (PB) will be 1-46. I've got that much taken cared of, but I'm having trouble with creating a restriction in the QP so there will be no double numbers. e.g 1, 5, 5, 32, 55.

Is it possible to create that restriction with an if then else statement?

#include <iostream> 
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int size;
int arr[5] = {0, 1, 2, 3, 4};
int i=0;
int PowerBall[1] = {0};


int QP()
{
    srand(time(NULL)); 
    for (int i=0; i<5; i++) /
    arr[i] = rand() % 56 + 1; 
    return 0;
}

int PB()
{
    srand(time(NULL)); 
    for (int i=0; i<5; i++) 
        PowerBall[i] = rand() % 56 + 1; 
    return 0;
}

void bubbleSort(int arr[], int size) //bubble sort function
{
    bool swapped = true; 
    int j = 0;
    int tmp;
    while (swapped)
    {
        swapped = false; 
        j++; 
        for (int i = 0; i < size - j; i++) { 
            if (arr[i+1] < arr[i]) { 
                tmp = arr[i]; 
                arr[i] = arr[i + 1]; 
                arr[i + 1] = tmp; 
                swapped = true; 
            }
        } 
    }
}


void print()
{
    for (int i = 0; i < 5; i++)
    cout << arr[i] << setw(8);
    cout << PowerBall[i] << endl;
}

int main()
{
    QP();
    bubbleSort(arr, 5);
    PB();
    print();
    system("pause");
    return 0;
}

Upvotes: 0

Views: 112

Answers (3)

oknsnl
oknsnl

Reputation: 351

Take numbers from array randomly qp_source_arr[56] which qp_source_arr[i]=i+1;

Then when you select one index randomly j=rand%56+1 arr[i]=gp_source_arr[j]; gp_source_arr[j]=-1;

After that point when you take -1 pick again until you get something is not -1.

I hope this will help you and i do not write the whole code because you should implement your own code and it seems like homework:D

Upvotes: 0

TonyK
TonyK

Reputation: 17124

One possibility is the Fisher-Yates shuffle. In your case, it looks like this:

int number_list[56];
for (int i = 0; i < 56; i++)
    number_list[i] = i + 1; // Initialise number_list

for (int i = 0; i < 5; i++) {
    int r = rand() % (56 - i);
    // Exchange number_list[i] and number_list[i + r]
    int t = number_list[i];
    number_list[i] = number_list[i + r];
    number_list[i + r] = t;
}

Now you can just read off the first five values from the number_list array: number_list[0],...,number_list[4]

I have posted a full working program at this ideone link.

Upvotes: 1

Nikos Athanasiou
Nikos Athanasiou

Reputation: 31549

Put your numbers in a set to exclude multiplicity (they'll also be automatically sorted)

int newNum; 
set<int> numSet;

for (int i(0); i < 5; ++i)
{
    // calculate a number
    while (!numSet.insert(newNum).second)
    {
        // calculate another number
        numSet.insert(newNum);
    }
}

Beware that once you surpass the power of the quickpick set (56) no new insertions can be made and you'll end up with an infinite loop (you might want to add a check there)

Upvotes: 1

Related Questions