This question already has an answer here:
- How to shuffle a std::vector? 4 answers
I need to generate a code of specified length, and it has to be random and non repetitive, so far, I have the random string but it's repeating the letters
Here is my code;
#include <string>
#include <cstdlib>
#include <ctime>
class random_text_generator
{
public:
random_text_generator(const std::string& str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
: m_str(str)
{
std::srand(std::time(0));
}
std::string operator ()(std::size_t len = 1)
{
std::string seq;
std::size_t siz = m_str.size();
if(siz)
while(len--)
seq.push_back(m_str[rand() % siz]);
return seq;
}
private:
std::string m_str;
};
#include <iostream>
int main(void)
{
using namespace std;
random_text_generator rtg;
for(size_t cnt = 0; cnt < 7; ++cnt)
{
cout << rtg(cnt) << endl;
}
}
Again, this code generates the string but it repeats some letters, for example the output would look like this
**OUTPUT**
A K X K Y
See that the K is repeating.
Aucun commentaire:
Enregistrer un commentaire