0

So I am trying to generate a random name between 8 and 16 characters. It doesn't need to actually spell a real name it is just a bunch of random letters.

So far I have it to generate a random first and last name but they are all sizes of 12

#include <iostream>
#include <ctime>

using namespace std;

void genID(char letters[26]);

int main()
{
srand(time(0));
char letters[26] = {'a', 'b' ,'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
genID(letters);
}
void genID(char letters[26])
{
string firstTemp, lastTemp;
for(int i = 0; i < 12; i++)
{
firstTemp = firstTemp + letters[rand() % 26];
lastTemp = lastTemp+ letters[rand() % 26];
}
}

I have code that handles printing out each first and last name respectively through each iteration I just wanted to show the main code of where I need to make the generation between 8 and 16 characters.

Thanks you!

PapaBlank
  • 3
  • 2
  • because of this statement `for(int i = 0; i < 12; i++)` – Harry Oct 30 '20 at 14:27
  • 1
    what exactly is your question? You already have random letters, now you just have to replace the hardcoded `12` with a random number between 8 and 16 – 463035818_is_not_an_ai Oct 30 '20 at 14:30
  • Although it's not really important for this little test program, `rand()` is pretty bad way to generate random numbers. Here's some info on a much better way: https://stackoverflow.com/questions/53040940/why-is-the-new-random-library-better-than-stdrand. – AVH Oct 30 '20 at 14:38
  • @idclev463035818: That's one way to do it, but it will mean that `aaaaaaaa` is a lot more likely than `aaaaaaaaaaaaaaaa`. – MSalters Oct 30 '20 at 14:46
  • @MSalters don't understand what you mean. When the number is picked from a uniform distribution in the range 8 till 16 then both are equally likely. What I overlooked was that there is a single loop for first and last name, but I suppose lenght of them should be independent – 463035818_is_not_an_ai Oct 30 '20 at 14:49
  • @idclev463035818: True, all lengths are equally likely. That means `sum(prob('aaaaaaaa') to prob('zzzzzzz'))` is 1/9, as is `sum(prob('aaaaaaaaaaaaaaaa') to prob('zzzzzzzzzzzzzzzz'))`. However, the latter is a sum over many more terms, so `prob('aaaaaaaaaaaaaaaa')` has to be a lot smaller. – MSalters Oct 30 '20 at 14:51
  • @MSalter you mean there are more names with 16 letters than there are with 8, so a uniform distribution puts a bias and long names are underrepresented. Yeah right. – 463035818_is_not_an_ai Oct 30 '20 at 14:53
  • @MSalters I mean it all depends on what distribution one wants to get in the end. There are no strict requirements here. I like your idea of using spaces – 463035818_is_not_an_ai Oct 30 '20 at 14:55

5 Answers5

0

To generate strings that have 8 to 16 letters, you will have to concatenate 8 to 16 letters.

To generate random integer between A and B (both inclusive), you can use A + rand() % (B - A + 1). (quality is not considered)

Try this:

void genID(char letters[26])
{
    string firstTemp, lastTemp;
    int firstLen = 8 + rand() % (16 - 8 + 1);
    int lastLen = 8 + rand() % (16 - 8 + 1);
    for(int i = 0; i < firstLen; i++)
    {
        firstTemp = firstTemp + letters[rand() % 26];
    }
    for(int i = 0; i < lastLen; i++)
    {
        lastTemp = lastTemp+ letters[rand() % 26];
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

The length itself should be random :

int len = rand() % 9 + 8 // produce a number between 8 and 16
for(int i = 0; i < len; i++)
{
    firstTemp = firstTemp + letters[rand() % 26];
    lastTemp = lastTemp+ letters[rand() % 26];
}
dspr
  • 2,383
  • 2
  • 15
  • 19
0

You are getting 12 characters because of your for loop statement. Specifically i < 12 condition. You should read more about how loops work, to understand this fundamental structure. For example, w3school has an pretty clear explanation.

To make the loop execute from 8 to 16 times (generate 8 to 16 characters), before starting a loop get random number for character count int letterCount = 8 + (rand() % 9);. Then, change the for loop statement to for (int i = 0; i < letterCount; i++). This should solve your problem.

Dharman
  • 30,962
  • 25
  • 85
  • 135
tsvedas
  • 1,049
  • 7
  • 18
0

You have to add a variable for the length and make it a random number from 8 to 16.

void genID(char letters[26])
{
    int length = rand() % 9 + 8;
    string firstTemp, lastTemp;
    for(int i = 0; i < length; i++)
    {
        firstTemp = firstTemp + letters[rand() % 26];
        lastTemp = lastTemp + letters[rand() % 26];
    }
}

However, if you just need a random combination of lowercase letters you can just use ASCII and the numerical values for the characters.

void genID()
{
    int length = rand() % 9 + 8;
    string firstTemp, lastTemp;
    for(int i = 0; i < length; i++)
    {
        firstTemp = rand() % 26 + 'a';
        lastTemp = rand() % 26 + 'a';
    }
}

Or, if you can't use ASCII, you may use a string instead of an array of chars.

#include <iostream>
#include <ctime>

void genID(string letters);

int main()
{
    srand(time(0));
    string letters = "abcdefghijklmnñopqrstuvwxyz";
    genID(letters);
}

void genID(string letters)
{
    int length = rand() % 9 + 8;
    string firstTemp, lastTemp;
    for(int i = 0; i < length; i++)
    {
        firstTemp = firstTemp + letters[rand() % 26];
        lastTemp = lastTemp + letters[rand() % 26];
    }
}

Finally, you may should read:

  1. Why is the use of rand() considered bad?
  2. Why is “using namespace std;” considered bad practice?

And do this:

#include <iostream>
#include <random>
#include <ctime>

void genID();

int main()
{
    genID();
}
void genID()
{
    std::default_random_engine eng(time(0));
    std::uniform_real_distribution<double> urd1(0, 26);
    std::uniform_real_distribution<double> urd2(0, 9);
    std::string firstTemp, lastTemp;
    int length = int (urd2(eng)) + 8;
    for(int i = 0; i < length; i++)
    {
        firstTemp += int (urd1(eng)) + 'a';
        lastTemp += int (urd1(eng)) + 'a';
    }
}
Gary Strivin'
  • 908
  • 7
  • 20
0

There are many ways in which you can solve it. Randomizing the loop length has been mentioned already. Another way is to generate two names of 8 characters each, but in the second case you would allow spaces. So you now get a 16 character name with 0-8 spaces. Remove those spaces, and you're left with 8-16 characters.

MSalters
  • 173,980
  • 10
  • 155
  • 350