// Simple random name generator // Copyright (C) 2005, Eli Bendersky // // License: LGPL (http://www.gnu.org/licenses/lgpl.txt) // #include #include #include using namespace std; // Extract a vector of tokens from a string (str) delimited by delims // vector tokenize(string str, string delims) { string::size_type start_index, end_index; vector ret; // Skip leading delimiters, to get to the first token start_index = str.find_first_not_of(delims); // While found a beginning of a new token // while (start_index != string::npos) { // Find the end of this token end_index = str.find_first_of(delims, start_index); // If this is the end of the string if (end_index == string::npos) end_index = str.length(); ret.push_back(str.substr(start_index, end_index - start_index)); // Find beginning of the next token start_index = str.find_first_not_of(delims, end_index); } return ret; } // Return a pseudo-random integer uniformly distributed // between 0 and max, inclusive // inline unsigned UniformRandom(unsigned max) { double d = rand() / (double) RAND_MAX; unsigned u = (unsigned) (d * (max + 1)); return (u == max + 1 ? max - 1 : u); } // Given a string with comma separated values (like "a,cd,k") // returns a random value. // string rand_elem(string csv) { vector elems = tokenize(csv, ","); return elems[UniformRandom(elems.size() - 1)]; } // Throws a bet with probability prob of success. Returns // true iff succeeded. // bool throw_with_prob(unsigned prob) { unsigned a_throw = 1 + UniformRandom(99); return (prob >= a_throw) ? true : false; } // A very rudimentary random name generator // string gen_random_name(void) { string vowelish = "a,o,e,i,u"; string vowelish_not_begin = "ew,ow,oo,oa,oi,oe,ae,ua"; string consonantish = "b,c,d,f,g,h,j,k,l,m,n,p,r,s,t,v,y,z,br,cl,gr,st,jh,tr,ty,dr,kr,ry,bt,sh,ch,pr"; string consonantish_not_begin = "mn,nh,rt,rs,rst,dn,nd,ds,bt,bs,bl,sk,vr,ks,sy,ny,vr,sht,ck"; char first_name_abbr = 'A' + char(UniformRandom(25)); bool last_was_vowel = false; string result = ""; // Generate first name (a letter + "_") result += first_name_abbr; result += "_"; // Generate beginning if (throw_with_prob(50)) { result += rand_elem(vowelish); last_was_vowel = true; } else { result += rand_elem(consonantish); last_was_vowel = false; } unsigned howmany_proceed = 2 + UniformRandom(3); for (unsigned i = 0; i < howmany_proceed; ++i) { if (last_was_vowel) { if (throw_with_prob(50)) result += rand_elem(consonantish); else result += rand_elem(consonantish_not_begin); } else { if (throw_with_prob(75)) result += rand_elem(vowelish); else result += rand_elem(vowelish_not_begin); } last_was_vowel = !last_was_vowel; } if (result.size() > 12) result = result.substr(0, 9 + UniformRandom(3)); // Eventually, capitalize the first letter of the surename // result[2] = toupper(result[2]); return result; } // Simple driver // int main() { srand(time(0)); for (int i = 0; i < 10; ++i) cout << gen_random_name() << endl; }