【发布时间】:2021-04-12 05:35:06
【问题描述】:
我想要做的是:
- 用户输入一个字符串(例如:“Hello”)
- 程序返回相同的字符串,但顺序是随机的(可以是“elHlo”或任何其他可能的顺序)
到目前为止,我已经编写了这段代码,但问题是有时随机生成的数字是相同的,因此它可能会打印两次或更多次相同的索引(字母):
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
cout << "Say something: ";
string text;
getline(cin, text);
cout << "\nChaotic text: ";
srand(time(0));
for(unsigned int j=0; j<text.length(); j++){
int randomLetter = rand()%text.length();
cout << text.at(randomLetter);
}
return 0;
}
谁能帮我解决它?
【问题讨论】: