Random Word Generation and Hint Functionality

I just wanted to share some of the extra stuff I am adding to my BullCowGame application. I hope people might find it useful. If you have any questions or any ideas, please feel free to comment on them.

Random Number Generation

Using the srand(time(0)) I was able to generate a seed, based off the users computer time, for the rand() function to generate a number between 1 and 10. I then used this number as an Identification value for a switch case statement. The time functionality comes from the #include Library.

Hint System

From there I was able to generate a hint system based of the random word. Using else if statements I created a bank of hints that is accessible through a "help" system making use of enums. Each round the player has the option to use up to 3 hints. I am currently considering adding a skip functionality, along with a few extra words to make the game a little more challenging and fun.

I would take a look at this for improvements on rand()

Secondly, why not create a class for the HiddenWord which will store all of this information? Holding the hints in a std::vector and have functions for populating the word/hints etc.

1 Like

Thanks for the info DanM, I’ll watch the video once I’m back from work and I will see about implementing std::vector into my code. I haven’t used vectors before, but it will be a fun learning experience trying to see how I can make it work.

New Random Number Generation

So I believe I understand why they suggest not using the rand() function. The function has a limited and unevenly spread range that can result in undesired effects. Also the common method of seeding can cause possible memory leaks and a high chance that data is uncalled. So I tried using the Mersenne Twister (mt19937) like they showed, and to be honest I like it much better. My word selection, although small, is showing a much better spread. I chose to use random_device over time(NULL) because it allowed for a different seed without the chance of repeat due to an answer being submitted too quickly.

Now I have also been studying a little into vectors, but do to my lack of knowledge I don’t really know how to accurately used them, so I will have to spend some time with them. As for now I’m going to use the current method I have been using. Thanks again DanM for linking me that video.

Looking pretty good, only real problem now is that you’re creating and seeding an engine everytime that function is called which isn’t particularly ideal and somewhat costly. Solution being either make it part of the class and seed it at construction or just simply make it a global variable as an RNG is another one of those exceptions to the general rule of “global variables are bad”.

Privacy & Terms