13,152,454 members (37,982 online)
Rate this:
See more:
I am looking for some help to create a random name generator with ASCII. I am new to C++ and really and not 100% sure how to attack this. "First and last names should be randomly generated and must begin with a capital letter. Additionally, the first letter should be a consonant. The remainder of the name should alternate between vowels and consonants, where every even numbered letter is a vowel and every odd numbered letter is a consonant." Any help would be GREAT!!.
Posted 13-Aug-09 0:30am
Updated 6-May-10 20:42pm
CPallini408.5K
v2
Sandeep Mewara 7-May-10 2:47am

How come 5 months old question suddenly pops here! Looks like already a lot has happened in it :-)

Rate this:

Solution 1

The way I'd approach it is like this:

1. Define functions that return random voewls and random consonants - here's a random vowel function:

`char RandomVowel()<br />{<br />   const char vowels[] = {'a', 'e', 'i', 'o', 'u'};<br />   // Use rand() to get a number between 0 and RAND_MAX. Dividing<br />   // by RAND_MAX+1 gives a number n such that 0 <= n < 1, so<br />   // multiplying by the number of characters in vowels gives a random<br />   // index into vowels<br />   const int index = (int)((double)rand()/(RAND_MAX+1) * sizeof(vowels));<br />   return vowels[index];<br />}`

2. Build up a string using alternating calls to the random vowel and consonant functions - note that I've remembered about the upper case first character!

```std::string name;<br />   name += (char)toupper(RandomConsonant());<br />   for(int i=1;i<desired-name-length;++i)<br />   {<br />      if (i%2==1)<br />         name += RandomVowel();<br />      else<br />         name += RandomConsonant();<br />   }
```

Obviously the desired name length should be chosen at random as well!

Rate this:

Solution 2

Here's my try:

```#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string name;
static const int namelen = 15;

static const char* letters[2] = { "bcdfghjklmnpqrstvwxyz", "aeiouy" };
static const letterlen[2] = { strlen(letters[0]), strlen(letters[1]) };

for (int i=0; i<namelen; i++)
name += letters[i%2][rand()%letterlen[i%2]];
name[0] = toupper(name[0]);

return 0;
}```
v2
Rate this:

Solution 4

Keith Barrow 14-May-11 10:05am

Come back Christian, this is why we need you!
Rate this:

Solution 3

`if(i%2==1)` can be simply written as `if(i&1)`

Top Experts
Last 24hrsThis month
 Richard MacCutchan 233 OriginalGriff 190 Graeme_Grant 145 Dave Kreskowiak 109 Jochen Arndt 90
 OriginalGriff 6,781 Graeme_Grant 5,246 ppolymorphe 2,179 Jochen Arndt 1,994 CPallini 1,950