宾果板:创造独特的价值
我无法生成唯一值,该值不会在该宾果板上重复。我的代码相对简单:我使用嵌套的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑用候选数字填充
std::vector
,然后对其执行std::random_shuffle
并获取前 N 个。Consider filling a
std::vector
with the candidate numbers, then perform astd::random_shuffle
on it and take the first N.我用于“生成 n 个唯一随机数”的常用方法是用数字的总范围(这里为 MIN -> MAX)填充一个向量,random_shuffle() 然后提取与我一样多的值需要从前面那个。我认为,如果性能极其关键,可能有稍微更有效的方法,但它似乎在我迄今为止需要的所有情况下都表现得很好。
像这样的东西
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
这是我为我的小项目想出的一些代码,
没什么花哨的,但它生成了唯一的数字,对我来说它满足了需求。
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.