将唯一的随机数插入向量中 [英] Inserting unique random numbers into a vector

查看:54
本文介绍了将唯一的随机数插入向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As a part of code for a certain game I want to generate 4 unique random numbers into a vector.

此代码适用于一定次数的重复播放,然后应用程序崩溃(无响应窗口).

This code works for some number of repeated plays and then application crashes (not responding window).

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

While I understand that if-condition prevents for-loop from inserting the same number into a vector, how much time does this for-loop takes until it generates unique numbers via rand() function? How srand(time(NULL)) and rand() exactly work together to create random values depending on the system time?

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

推荐答案

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

The problem is you always push back the random value in your vector before checking if it's valid. Let's say your program generates these random value in order:

2、6、6、7、9、10

2, 6, 6, 7, 9, 10

结果是你将插入 2 (i == 2), 6 (i == 3), 6 (i == 4),然后意识到 6 重复了两次,所以你返回一次迭代 (i ==3),但你的两个六点仍然在你的向量中.所以现在您将添加 7 (i == 4) 并且您将退出 for 循环,向量中有 5 个值.

What happens is you will insert 2 (i == 2), 6 (i == 3), 6 (i == 4), then realize 6 is repeated twice, so you go back one iteration (i == 3), but both your sixes are still in your vector. So now you will add 7 (i == 4) and you will exit the for loop with 5 values in your vector.

然后,当您评估 do-while 条件时,您的 answer.size() 永远不会等于 4,因为它已经等于 5.您现在陷入了无限循环,并且您的应用程序在消耗完所有内容后崩溃来自向量的可用内存无限增长.

Then when you evaluate your do-while condition, your answer.size() won't ever equal 4 because it already is equal to 5. You are now stuck in an infinite loop, and your application crashes when it consumes all the available memory from your vector growing infinitely.

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

Also, you appear to have an error in your logic. To make sure you don't have a repeated value (and you are stuck with vectors), you should not only validate the last inserted value but the whole vector. Like this:

#include <algorithm>

if ( std::find(vector.begin(), vector.end(), item) != vector.end() )
   do_this();
else
   do that();

这篇关于将唯一的随机数插入向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆