在向量中插入唯一的随机数

Inserting unique random numbers into a vector

本文关键字:随机数 唯一 插入 向量      更新时间:2023-10-16

作为某个游戏的代码的一部分,我想在一个向量中生成4个唯一的随机数。

此代码适用于一些重复播放,然后应用程序崩溃(没有响应窗口)。

虽然我知道,如果条件阻止for循环将相同的数字插入到向量中,那么这个for循环需要多少时间才能通过rand()函数生成唯一的数字?srand(time(NULL))rand()如何根据系统时间精确地协同工作以创建随机值?

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
//plays bulls and cows

int main() {
srand(time(NULL));
string play="yes";
int nums=4;       // number of values in an answer (must NOT exceed 10)
vector<int> answer;

while (play=="yes" || play=="YES" || play=="Y" || play=="Yes" || play=="y") { //plays the game
answer.push_back(rand()%10+1);
do {                              //fills vector with unique random numbers
for (int i=1; i<nums; i++) {
answer.push_back(rand()%10+1);
if (answer[i]==answer[i-1]) {
i=i-1;
continue;
}
}
} while (answer.size()!=nums);
for (int i=0; i<nums; i++) {
cout<<answer[i];
}
cout<<"Do you want to play again?"<<'n';
cin>>play;
answer.clear();
} //game ends

if (play=="no" || play=="n" || play=="No" || play=="NO" || play=="N") { //terminates and checks for exceptions
cout<<"Thank you for playing!"<<'n';
return 0;
} else {
cerr<<"Error: wrong input. Terminating."<<'n';
return 0;
}
return 0; //safety return
}

为什么要在answer中添加新的try而不是临时变量。如果变量有效,则将其添加到答案中。在您的情况下,i始终保持在1

while (play=="yes" || play=="YES" || play=="Y" || play=="Yes" || play=="y") { //plays the game
int last_try=rand()%10+1;
answer.push_back(last_try);
do { //fills vector with unique random numbers
int new_try=rand()%10+1;
if (last_try!=new_try)
{
answer.push_back(new_try);
last_try=new_try;
}
} while (answer.size()!=nums);

for (int i=0; i<nums; i++)
{
cout<<answer[i]<<"n";
}
cout<<"Do you want to play again?"<<'n';
cin>>play;
answer.clear();
} //game ends

假设您必须使用std::vector(而不是std::set)。用随机数填充向量的最简单方法是检查该数字是否已经被"看到"——如果没有,则将其添加到向量中。

这可以通过使用bool的数组作为辅助来确定是否已经看到该数字来实现:

#include <vector>
#include <iostream>
#include <cstdlib>
int main()
{
std::vector<int> answer;
int num = 4;
// 10 numbers
bool seen[10] = {false};
// keeps track of numbers added
int numsAdded = 0;
while (numsAdded < num)
{
int numRand = rand()%10;
if ( !seen[numRand] )
{
// not seen, so add it to vector and update bool array and
// numsAdded
answer.push_back(numRand + 1);
seen[num] = true;
++numsAdded;
}
}
for (size_t i = 0; i < num; ++i)
std::cout << answer[i] << " ";
}

实际示例

问题是,在检查向量中的随机值是否有效之前,总是将其推回。假设您的程序按顺序生成这些随机值:

2、6、6、7、9、10

发生的情况是,你将插入2(i==2),6(i==3),6。因此,现在您将添加7(i==4),并在向量中使用5个值退出for循环。

然后,当你评估do-while条件时,你的答案.size()永远不会等于4,因为它已经等于5了。您现在陷入了一个无限循环,当您的应用程序消耗了无限增长的向量中的所有可用内存时,它就会崩溃。

此外,您的逻辑似乎有错误。为了确保没有重复的值(并且使用向量),您不仅应该验证最后插入的值,还应该验证整个向量。像这样:

#include <algorithm>
if ( std::find(vector.begin(), vector.end(), item) != vector.end() )
do_this();
else
do that();