在向量中插入唯一的随机数
Inserting unique random numbers into a vector
作为某个游戏的代码的一部分,我想在一个向量中生成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();
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 整数不会重复超过随机数
- 何时在引用或唯一指针上使用移动语义
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 计算排序向量的向量中唯一值的计数
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 通过组合不同的类型来创建唯一的id
- if数组上的随机数
- 具有随机数的唯一向量
- 使用具有唯一随机数的结构数组创建多个对象
- 在C++中生成唯一的随机数
- 在向量中插入唯一的随机数
- 在多维数组中生成唯一的随机数C++
- 生成唯一的多个随机数
- 从一个特定的范围中生成一定数量的唯一随机数
- 在区间[0,n-1]中生成k个唯一随机数
- 输出 4 组 7 个升序有序和唯一随机数
- 在数组C++中生成唯一随机数
- 试图在每次迭代中产生一个唯一的随机数序列
- 使用线程生成唯一的随机数