How to do a random word generator?
I have to use a prototype string randomword(int length) with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <random>
#include <string>
#include <time.h>
using namespace std;
const int nalphas = 26;
const string emptystring = "";
default_random_engine e((unsigned)time(0));
uniform_int_distribution<int> u(0, nalphas - 1);
string randomword(int length);
int main()
{
for (int counter = 1; counter <= 64; counter++)
cout << randomword(4) << endl;
system("pause");
return 0;
}
|
to generate a string of random letters of a given length.
Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <ctime>
#include <random>
#include <string>
std::string random_string( std::size_t length )
{
static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz" ;
static std::default_random_engine rng( std::time(nullptr) ) ;
static std::uniform_int_distribution<std::size_t> distribution( 0, alphabet.size() - 1 ) ;
std::string str ;
while( str.size() < length ) str += alphabet[ distribution(rng) ] ;
return str ;
}
int main()
{
for( std::size_t i = 10 ; i < 40 ; i += 3 ) std::cout << random_string(i) << '\n' ;
}
|
http://coliru.stacked-crooked.com/a/96612e610957d87e
Those generate random strings of letters.
If you are looking for something more along the lines of creating plausible words, google around "random name generator".
A very good one, written in Tcl/Tk, is here:
http://billposer.org/Software/WordGenerator.html
A very simple example, without any explanation for choice of letters, is here:
http://wiki.tcl.tk/13708
Hope this helps.
Topic archived. No new replies allowed.