C++ 我试着从向量中随机抽取一个词,但每次都是相同的词
正如我在标题上提到的; 我有一个向量,它存储了一堆单词(大约2000个单词),在项目的一部分,我希望从向量中随机抽取一个单词并显示出来。(这个项目是找词的)我有两门课,一门是主课,另一门是从vector中提取单词。我把代码附在下面。一定有什么东西逃过了我的注意,但不知怎的,我弄不明白是什么东西C++ 我试着从向量中随机抽取一个词,但每次都是相同的词,c++,vector,random,C++,Vector,Random,正如我在标题上提到的; 我有一个向量,它存储了一堆单词(大约2000个单词),在项目的一部分,我希望从向量中随机抽取一个单词并显示出来。(这个项目是找词的)我有两门课,一门是主课,另一门是从vector中提取单词。我把代码附在下面。一定有什么东西逃过了我的注意,但不知怎的,我弄不明白是什么东西 #include <iostream> #include <vector>//must be #include <string>//must be #include &
#include <iostream>
#include <vector>//must be
#include <string>//must be
#include "Dictionary.h"
#include <set>
using namespace std;
int main() //main
{
vector<string> words;
Dictionary dict(words);
words = dict.setElements();
string randomWord = dict.getaRandomWord(words); //this line is for generate a random word
cout << randomWord;
return 0;
}
class Dictionary //dictionary class
{
public:
Dictionary(::vector<string> a) {
//getWordsFromFile() = a;
}
/* public :vector<string> getWordsFromFile() {
vector<string> words;
ifstream inFile;
string tmp;
inFile.open("words.txt");
while (!inFile.eof()) {
inFile >> tmp;
words.push_back(tmp);
}
return words;
}*/
vector<string> setElements() {
// Declaring Vector of String type
set<string> wordsSet;
string buffer;
vector<string> wordsVector;
ifstream inFile;
string tmp;
inFile.open("words.txt");
while (!inFile.eof()) {
inFile >> buffer;
wordsSet.insert(buffer);
}
for (set<string>::iterator it = wordsSet.begin(); it != wordsSet.end(); ++it) {
wordsVector.push_back(*it);
}
/*for (int i = 0; i < wordsVector.size(); i++) {
cout << wordsVector.at(i)<<endl; //its for displaying the elements of the vector
}*/
return wordsVector;
}
string getaRandomWord(vector<string> a) { //function that has supposed to be generate a random word
vector<string> currentVector = a;
string randomWord;
int randomNumber = rand() % currentVector.size();
randomWord = currentVector.at(randomNumber);
return randomWord;
}
};
#包括
#include//必须是
#include//必须是
#包括“Dictionary.h”
#包括
使用名称空间std;
int main()//main
{
向量词;
词典词典(单词);
words=dict.setElements();
string randomWord=dict.getaRandomWord(words);//此行用于生成随机单词
cout>tmp;
字。推回(tmp);
}
返回单词;
}*/
向量集合元素(){
//声明字符串类型的向量
设置单词集;
字符串缓冲区;
向量词向量;
河流充填;
串tmp;
infle.open(“words.txt”);
而(!infle.eof()){
填充>>缓冲区;
wordsSet.insert(缓冲区);
}
for(set::iterator it=wordsSet.begin();it!=wordsSet.end();++it){
wordsVector.push_back(*it);
}
/*对于(int i=0;i cout为了获得更多的随机数,您需要给它一个值,以使随机值成为基础,否则它将始终给出相同的值。为了获得不同的值,您可能需要为随机数生成器添加种子,以获得更大的分布。这是一个很好的解释,或者您可以检查文档。
您想要使用的是std::srand(std::time(null))
在程序开始时,通常在构造函数中执行一次。这将为程序的每次运行生成一组不同的伪随机数。在开始使用随机数生成器之前,您需要为其设置种子,否则它将始终生成相同的序列。这是使用当前时间。你的意思是我应该创建一个生成随机数的新函数吗?如果是,为什么要使用此代码(int randomNumber=rand()%currentVector.size();)不需要生成一个数字吗?它不是每次都会产生不同的数字吗?它是C++的吗?我是C++的新手,我在C++之前才知道java,但是没有这样的情况。PRNGs就是这样工作的。其他语言可能会为你做。我也不明白为什么你要在函数中复制向量。它工作了。我是说,我把种子放进字典的容器里,看看会发生什么。我刚刚摆脱了“老化”每次都会生成不同的单词,但这些单词之间似乎仍然有一个顺序。所以,生成的单词就像:浩劫-沉重-棚屋…你看到它们之间的联系了吗?它们是按照字母顺序生成的,因为它们之间有一定的间隔。为什么会发生这种情况?顺便感谢你的表达!
class Dictionary
{
public:
Dictionary(::vector<string> a) {
//getWordsFromFile() = a;
srand(time(nullptr));
.
.
.
}