Finished section 2, added array of strings and random string picker

So I wanted to add some extra functionality like Ben encouraged. I chose that wanted to make a predefined list of isograms that would be picked randomly in the Reset() function, to get a different word every time the game is played.

I ended up adding this code, after using c++.com and stackoverflow as resources:

#include <random>

FString FBullCowGame::GetRandomIsogram() const //this function is called from the Reset() function
{
	const int32 arrayLength = 12;
	FString isogramArray[arrayLength] = { "ant", "key", "one", "shoe", "claw", "star", "plant", "bread", "phone", "planet", "smoker", "chains" };

	// pick a random string from the array
	std::default_random_engine generator; 
	std::uniform_int_distribution<int32> distribution(0, arrayLength-1);
	int32 stringIndex = distribution(generator); // generates number in the distribution range
	FString isogram = isogramArray[stringIndex];
	return isogram;
}

So it seemed to work the first time i started the game. However, I had two problems:
1: the same word got picked every time lost/won or even when i restarted the game.
2: I seem to have used a “magic number” (const int32 arrayLength = 12;). This number has to be edited every time i add a word to my array. I would love some help for improving this.

So i did some more research, on the same resources. I found a solution for problem 1: I needed a seed to use with my generator to prevent getting the same result every time:

#include <random>
#include <chrono> //required for seed for randomizer

FString FBullCowGame::GetRandomIsogram() const //this function is called from the Reset() function
{
	const int32 arrayLength = 12;
	FString isogramArray[arrayLength] = { "ant", "key", "one", "shoe", "claw", "star", "plant", "bread", "phone", "planet", "smoker", "chains" };
	
	// construct a time based seed
	int32 seed = std::chrono::system_clock::now().time_since_epoch().count();

	// pick a random string from the array
	std::default_random_engine generator(seed); // use the seed to avoid getting same result every time.
	std::uniform_int_distribution<int32> distribution(0, arrayLength-1);
	int32 stringIndex = distribution(generator); // generates number in the distribution range
	FString isogram = isogramArray[stringIndex];
	return isogram;
}

So after implementing this seed my random word picker worked perfectly.
What I learned:
-I am starting to agree with the people that say “Learn C++ before (insert random programming language here)”. I really think I am getting a better understanding for programming now, compared to using higher level programming.
-Data structures in C++ is not as obvious as in C# or other programming languages i tried. I need to research more about this later, and hopefully fix my problem number 2.
-Random number generators will generate the same number every time if they don’t have a seed. I took the seed for granted when i previously used game engines that has this functionality built in. My seed seems to be based on the clock from local computer, which is good enough for this project. But i can see that this would not be a good solution for e.g. Blizzard games like WoW and Diablo 3. Anyone know how their RNG seed works?

Privacy & Terms