宾果板:创造独特的价值

发布于 2024-12-06 08:07:55 字数 2205 浏览 0 评论 0原文

我无法生成唯一值,该值不会在该宾果板上重复。我的代码相对简单:我使用嵌套的 for 循环通过一些 print 语句生成值;在每次嵌套迭代时,我都会检查生成的值是否存在于数组中。如果存在则返回true,生成的值选择一个新的随机数。我认为通过在每次迭代时启动 srand() 并使用循环中的计数作为其种子,我将能够实现这一目标。不幸的是,这似乎不太可能。

这是如何实现的?

我的代码:

#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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桜花祭 2024-12-13 08:07:55

考虑用候选数字填充 std::vector,然后对其执行 std::random_shuffle 并获取前 N 个。

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

十秒萌定你 2024-12-13 08:07:55

我用于“生成 n 个唯一随机数”的常用方法是用数字的总范围(这里为 MIN -> MAX)填充一个向量,random_shuffle() 然后提取与我一样多的值需要从前面那个。我认为,如果性能极其关键,可能有稍微更有效的方法,但它似乎在我迄今为止需要的所有情况下都表现得很好。

像这样的东西

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();
    }
}

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();
    }
}
荭秂 2024-12-13 08:07:55

这是我为我的小项目想出的一些代码,

没什么花哨的,但它生成了唯一的数字,对我来说它满足了需求。

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                 
}     

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                 
}     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文