How to populate a string vector randomly

Hello,
I have trouble making my function that populate a string vector with a size of 10 and fill it with 4 different stings randomly and those strings can be duplicated within that vector.

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 <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

void vectorFill(vector <string>& container)
{
	container.push_back("water");
	container.push_back("fire");
	container.push_back("earth");
	container.push_back("air");


}
void printVector(const vector<string>& container)
{
	for (unsigned int i = 0; i < container.size(); i++)
	{
		if (i < 1) { cout << "\nInventory: " << container[i]; }
		else { cout << ", " << container[i]; }
	}
	cout << "\n";
}

int main()
{
	srand(unsigned int(time(NULL)));
	vector<string> Start;
	vectorFill(Start);
	printVector(Start);
}
Hello Growthra,

If I understand what you want consider this:
1
2
3
4
5
6
7
8
9
10
void vectorFill(vector <string>& container)
{
    constexpr int MAXSIZE{ 10 }, MODVAL{ 4 };
    const std::string ELEMENTS{}{"water", "fire", "earth", "air"};

    for (int idx = 0; idx < MAXSIZE; idx++)
    {
        container.push_back(ELEMENTS[rand() % MODVAL]);
    }
}

This is untested, but I believe it should work.

Also the more up-to-date way of writing "srand" is: srand(static_cast<unsigned int>(time(nullptr)));. In case you did not know. This works from C++11 on.

Andy
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
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <ctime>

void vectorFill(std::vector<std::string>& container)
{
	constexpr size_t noElems {10};
	constexpr const char* const strs[] {"Water", "Fire", "Earth", "Air"};

	container.resize(noElems);

	for (auto& c : container)
		c = strs[std::rand() % std::size(strs)];
}

void printVector(const std::vector<std::string>& container)
{
	for (size_t i = 0; i < container.size(); ++i)
		if (i == 0)
			std::cout << "\nInventory: " << container[i];
		else
			std::cout << ", " << container[i];

	std::cout << '\n';
}

int main()
{
	std::srand(static_cast<unsigned int>(time(nullptr)));

	std::vector<std::string> Start;

	vectorFill(Start);
	printVector(Start);
}



Inventory: Air, Earth, Water, Water, Water, Fire, Fire, Earth, Earth, Earth

Hello Growthra,

After testing I found I made a typo on line 4. "ELEMENTS" should be an array of strings. The empty {} should be [].

When I compiled the code "push_back" did not work, but "emplace_back" does.

This is an example of what I get:

Inventory: air, water, fire, air, air, water, earth, fire, air, earth

Inventory: earth, water, earth, air, earth, water, air, earth, water, water

Inventory: air, earth, fire, fire, fire, fire, earth, fire, fire, air


You can see that "rand" has the habit of returning the same number several times especially when the number of choices is small.

You may want to consider using "<random>" header and the newer RNGs.

Andy

Edit:
Last edited on
Thanks, Andy
the way you did it gave me the idea of a very caveman brutish way to do it :D.

1
2
3
4
5
6
7
8
void filler(vector <string>& Blah)
{
	int size = 10;
	string words[4] = { "apple", "banana", "potato", "tomato" };

	for (int i = 0; i < size; i++)
	{ Blah.push_back(words[rand() % 4]); }
}
Topic archived. No new replies allowed.