12,406,687 members (55,502 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
CPallini386.6K
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()
{
const char vowels[] = {'a', 'e', 'i', 'o', 'u'};
// Use rand() to get a number between 0 and RAND_MAX. Dividing
// by RAND_MAX+1 gives a number n such that 0 <= n < 1, so
// multiplying by the number of characters in vowels gives a random
// index into vowels
const int index = (int)((double)rand()/(RAND_MAX+1) * sizeof(vowels));
return vowels[index];
}```

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;
name += (char)toupper(RandomConsonant());
for(int i=1;i<desired-name-length;++i)
{
if (i%2==1)
name += RandomVowel();
else
name += RandomConsonant();
}
```

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
 OriginalGriff 270 Karthik Bangalore 204 Richard MacCutchan 140 BillWoodruff 75 Afzaal Ahmad Zeeshan 75
 OriginalGriff 7,957 Karthik Bangalore 3,531 ppolymorphe 3,400 Richard MacCutchan 2,612 F-ES Sitecore 2,300