开发者

Bingo Board: Generating unique values

开发者 https://www.devze.com 2023-04-07 00:31 出处:网络
I\'m having trouble generating unique values which do NOT repeat for this bingo board. My code is relatively simple: I use a nested for loop to generate the values with some print statements; upon eac

I'm having trouble generating unique values which do NOT repeat for this bingo board. My code is relatively simple: I use a nested for loop to generate the values with some print statements; upon each nested iteration, I check to see if the value generated exists within the array. If it exists, it returns true, and the generated value selects a new random number. I thought that by initiating srand() upon each iteration, and using the count in the loop as its seed, I would be able to achieve this. Unfortunately, It doesn't seem very possible.

How is this achieved?

My code:

#define MAX 100
#define MIN 1

using std::vector;

bool Board::checkValues(unsigned int array[], unsigned int valueToCheck)
{
    int len = sizeof(array) / sizeof(int);

    bool numberExists = false;

    static int repeatCount = 0;

    for(int i = 1; i < len; i++)
    {
        if (valueToCheck == array[i])
        {
            numberExists = true;
            repeatCount++;
            break;
        }
开发者_如何转开发    }

    return numberExists;
}

Board::Board(unsigned int numberOfRows, unsigned int numberOfColumns)
{
    this->numRows = numberOfRows;
    this->numColumns = numberOfColumns;

    for (int i = 0; i < this->numRows; i++)
    {
        this->board.push_back(vector<unsigned int>(this->numColumns, 0));
    }

    this->valuesVisited[numberOfRows * numberOfColumns];
}

void Board::generate()
{
    int repeatCount = 0;

    for(int i = 0; i < this->numRows; i++)
    {
        bool atMid = false;

        if (i == this->numRows / 2 - 1)
        {
            atMid = true;
        }

        for(int j = 0; j < this->numColumns; j++)
        {
            if (atMid && j == this->numColumns / 2 - 1)
            {
                printf(" Free ");
                continue;
            }

            int seed = (i + 1) * (j + 1);

            unsigned int randNumber = generateRand(MIN, MAX, seed);

            bool numberExists = checkValues(this->valuesVisited, randNumber);

            if (numberExists)
            {
                //int equation = (randNumber % 10) + (i * j) / (randNumber + randNumber);

                randNumber = generateRand(MIN, MAX, seed) - (i * j);
                repeatCount++;
            }

            this->valuesVisited[(i + 1) * (j + 1)] = randNumber;

            this->board[i][j] = randNumber;

            printf(" %d ", board[i][j]);
        }

        std::cout << "\n\n";
    }

    printf("You have %d repeats", repeatCount);
}


Consider filling a std::vector with the candidate numbers, then perform a std::random_shuffle on it and take the first N.


The usual approach I use for this "generate n unique random numbers" is to fill a vector with the total range of numbers (for you here, MIN -> MAX), random_shuffle() that and then just pull as many values as I need from the front that. I think there are probably slightly more efficient ways if performance is ultra-critical, but it seems to do pretty well in all the situations I've needed so far.

Something like

std::vector<int> numbers;
int index = MIN;
std::generate_n(back_inserter(numbers), MAX - MIN + 1,
    [&](){return index++;});

std::random_shuffle(numbers.begin(), numbers.end());

for(int i = 0; i < this->numRows; i++)
{
    for(int j = 0; j < this->numColumns; j++)
    {
        this->board[i][j] = numbers.back();
        numbers.pop_back();
    }
}


This is some code I came up with for my little project

Nothing fancy but it generates unique numbers and for me it filled a need.

for (int a = 0; a <= 89; a++) //populate the array with numbers 1-90
{
    bNumbers[a].Number = a + 1;
}
for (int a = 0; a < bNumbers.Length; a++) //swap positions of the generated numbers
{
    int rBingo = bMain.rndNum.Next(a, bNumbers.Length); //generate random number
    // swap numbers round in the array
    int tmpNum = bNumbers.Number;
    bNumbers.Number = bNumbers[rBingo].Number;
    bNumbers[rBingo].Number = tmpNum;
    //end of swap                 
}     
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号