【问题标题】:How to generate random numbers without duplicates using a loop in c++?如何使用c ++中的循环生成不重复的随机数?
【发布时间】:2020-07-09 10:31:01
【问题描述】:

我正在编写一个没有重复的随机名称生成器。我想在没有像 algorithm 之类的其他库的情况下做到这一点,因为我想了解如何做到这一点。有没有办法用循环或向量来做到这一点?

我想将已经存在的名称添加到向量中,然后使用 while 循环检查向量的所有元素,但我不知道该怎么做。

    #include <windows.h>
    #include <conio.h>
    #include <vector>
    #include <ctime>
    #include <iostream>
    #include <ctime>
    
using namespace std;

int main() {
 srand((unsigned)time(0));   
    int names = 1;
    int a = 0;
    int x = 0;
    vector <string> vectornames;
    cout << "How many names would you like to add: ";
int amount_of_names;
cin >> amount_of_names;
    while (a < amount_of_names) {

 
cout << "Enter name " << names << ": ";
string name;
cin >> name;
vectornames.push_back(name);
names++;       
a++;   


    
   
       
    }
    while (x < amount_of_names) {
     cout << "\n" << vectornames.at(x);
    x++;   }
    
    cout << "\npress enter to continue: ";
    getch();
    system("cls");
    int z = 0;
while (z < amount_of_names) { 
     
     int random_number = (rand() % amount_of_names);
     if (vectornames.at(random_number) != vectornames.at(z)) {
    cout << "Hello ";
    cout << vectornames.at(z);
    cout << "! You will get " << vectornames.at(random_number) << "\n";
    vectornames.push_back(vectornames.at(random_number));
    
    
    getch();
     
    z++;
}}

    cout << "\n\n";
  

  return 0;
    }

【问题讨论】:

  • 也许是std::unordered_set 的字母?循环生成,直到获得所需的长度。并且请不要使用srandrand,一定有更好的classes available in the standard library
  • 完成后,用1, 2, 3, 4 ... 之类的数字序列填充vector&lt;int&gt;,然后随机填充swap 元素。您现在有一个随机的数字序列可供选择。 int random_number = vec.back(); vec.pop_back();(直到 vector 为空)。
  • 您可以使用<random>random_shuffle 对数字向量进行洗牌。
  • @Ususwoa &lt;algorithm&gt;&lt;random> 和其他人是标准库的一部分,就像 &lt;vector&gt; 一样。
  • 为什么rand() 被允许,即使它经常被建议避免它并且random 是不允许的?

标签: c++ loops vector while-loop


【解决方案1】:

最后一部分有一些问题,你选择了多少你输入的名字,不知道这是不是故意的,就说是吧。您将选定的名称添加到同一个向量中,您没有正确检查重复项。

while (z < amount_of_names) {    <-- same amount as was entered in original loop
  int random_number = (rand() % amount_of_names);
  if (vectornames.at(random_number) != vectornames.at(z)) { <--- doesn't check for duplicates
    cout << "Hello ";
    cout << vectornames.at(z);
    cout << "! You will get " << vectornames.at(random_number) << "\n";
    vectornames.push_back(vectornames.at(random_number)); <---- adding to same vector
    getch();
    z++;
  }
}

让我们看看不使用算法可以做什么......这意味着我们需要实现std::find 或使用其他一些结构来检查已经选择的内容。

!!!警告未经测试的代码!!!

bool Find(const std::string& needle, const std::vector<std::string>& haystack) const {
  for(const auto& straw : haystack) {
    if (straw == needle) {
      return true;
    }
  }
  return false;
}

并且更新了代码,它使用不同的向量来存储使用的名称。

std::vector<std::string> used;

while (z < amount_of_names) {
  int random_number = (rand() % amount_of_names);
  
  if (!Find(vectornames.at(random_number), used)) { 
    cout << "Hello ";
    cout << vectornames.at(z);
    cout << "! You will get " << vectornames.at(random_number) << "\n";
    used.push_back(vectornames.at(random_number));
    getch();
    z++;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 2011-06-23
    • 2018-06-29
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多