Need to store 5000 Randomly Generated Names in Array

I need to store 5000 randomly generated names in an array. I have made my generator, however, I can't seem to figure out how to get 5000 random names.

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>

using namespace std;

string RandomFirstGen(int namelength);

int main(){

srand(time(NULL));
int namelength = rand()%(16-8+1)+8;
cout<<"name is: "<<RandomFirstGen(namelength)<<endl;

return 0;
}

string RandomFirstGen(int namelength){
for (int i=0; i<=5000 ; i++){
const int MAX = 26;
char alphabet[MAX] = {
'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'};
string name = "";
for (int i = 0; i < namelength; i++)
name = name + alphabet[rand() % MAX];
return name;

}
}
1
2
3
4
5
6
for (int i = 0 ; i < 5000; i++)
{
  // generate a random name (i.e. call your function here)

  // do something with it
}


Your current code appears to have a function that returns after generating one random name. With a crazy for loop inside your function. This makes no sense.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>

using namespace std;

string RandomFirstGen(int namelength);

int main(){

srand(time(NULL));
int namelength = rand()%(16-8+1)+8;
cout<<"name is: "<<RandomFirstGen(namelength)<<endl;

return 0;
}

string RandomFirstGen(int namelength){
for (int i=0; i<=5000 ; i++){
const int MAX = 26;
char alphabet[MAX] = {
'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'};
string name = "";
for (int i = 0; i < namelength; i++)
name = name + alphabet[rand() % MAX];
return name;

}
}

Just formatting so i can see what you are doing clearer. Plz use code tags.
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <random>

int main()
{
	constexpr size_t noNames {5000};
	constexpr size_t minName {5};
	constexpr size_t maxName {15};

	std::string names[noNames];

	auto rand {std::mt19937 {std::random_device {}()}};
	auto namsize {std::uniform_int_distribution<size_t> {minName, maxName}};
	auto letter {std::uniform_int_distribution<size_t> {0, 25}};

	for (size_t n = 0; n < noNames; ++n) {
		std::string name;

		for (size_t m = 0, nl = namsize(rand); m < nl; ++m)
			name += letter(rand) + 'a';

		names[n] = name;
	}

        // Names now has the 5000 random names with lengths between minName and maxName
}

Last edited on
So i kinda just tweaked to code with comments so you can analyse it. Hope this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>

using namespace std;

string RandomFirstGen(int namelength);

int main()
{
	srand(time(NULL));
	int namelength = (rand() % 9) + 8; // Not sure why you had all the arithmetic here so i simplified to what i thought you may mean.

	for (int i = 0; i < 5000; i++) // changed to < and not <= else you are actually counting to 5001
	{
		cout << "name is: " << RandomFirstGen(namelength) << endl;
	}
	// Notice this function is getting called 5000 times instead of calling once and expecting it to return a name 5000 times.
	// functions will only return once every time they are called.

	return 0;
}

string RandomFirstGen(int namelength)
{
	const int MAX = 26;
	char alphabet[MAX] =
	{
		'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'
	};

	string name;
	for (int i = 0; i < namelength; i++)
	{
		name.push_back(alphabet[rand() % MAX]); // Best to use the container pushback function.
	}
	return name;
}


what was the range for the name length supposed to be because I think I butchered it?
Last edited on
Hello RxIsCritical,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Although everyone's suggestions are good.

If you can use what they have said it helps if you say what you can use and or post the instructions that was given to you so everyone will know what you have to do.

I came up with this trying to keep what you started with. Some things have been rearranged with my best guess at what you actually want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

constexpr int MAXSIZE{ 5000 };  // <--- Can be used in both functions.

void RandomNameGen(std::string names[]);  // <--- Changed function name.

int main()
{
    std::string names[MAXSIZE];

    //srand(time(NULL));
    srand(static_cast<unsigned int>(time(nullptr)));

    RandomNameGen(names);  // <--- Changed function name.
    //cout << "name is: " << RandomFirstGen() << endl;  // <--- As mentioned this will only produce 1 name.

    for (size_t idx = 0; idx < 20; idx++) // <--- Used for testing. Comment or remove when finished.
    {
        std::cout << std::setw(4) << idx + 1 << ". " << names[idx] << '\n';
    }

    return 0;  // <--- Not required, but makes a good break point.
}

void RandomNameGen(std::string names[])  // <--- Changed function name.
{
    //const int MAX = 26;
    constexpr int MAX{ 26 };

    const char alphabet[MAX]  // <--- Could use a "std::string" just as easily.
    {
        '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' 
    };
    
    for (int i = 0, idx = 0; i < MAXSIZE; i++, idx++)  // <--- Changed. The <= would generate 5001 names putting you past the end of the array.
    {
        string name;
        int namelength = rand() % (16 - 8 + 1) + 4 ;
        
        for (int i = 0; i < namelength; i++)
        {
            rand();  // <--- Added. Sometimes it helps.

            name = name + alphabet[rand() % MAX];
        }

        names[idx] = name;
    }
}

I rearranged your header files based on:
seeplus wrote:

any required #define statements
std:: headers (eg iostream)
c headers (eg conio.h)
windows headers (eg windows.h)
3rd party headers
user headers


I moved things around in the function to make better use of the function. Also renaming the function describes what it does better, but still may be improved on.

Consider this and see what you think.

Andy
 
name = name + alphabet[rand() % MAX];


I know this is what the OP used, but you don't need alphabet.

 
name += rand() % MAX + 'a';


as rand() % MAX gives a number between 0 and 25 which when added to 'a' gives a 'char' between 'a' and 'z'.
Last edited on
@seeplus yes you are right.
But as a novice coder myself I tend to think altering the code as little as possible makes it somewhat clearer for a beginner to understand. While your original solution is good it also throws in a lot keywords ect. that a beginner likely has no idea what they do.
Topic archived. No new replies allowed.