My bull cow game - With levels and random words!

Hey guys, here’s the code for my slightly modified bull cow game. I left a couple of TODOs in there as areas for future improvement. I’m pretty eager to get the the next section so I might save those changes for later.

Here’s a question for anyone who knows their way around C++, I was trying to generate a random int using “rand() % 4” but the random numbers given each level seem to be the same every play through, what’s the best practice when generating random numbers?

Cheers!

Git Link: https://github.com/GregDevProjects/Bull_Cow_Game

It’s probably because you aren’t seeding rand. Though there’s a whole talk on why you shouldn’t use rand. For a basic intro on how to use C++11’s random header:

#include <random>
#include <iostream>

int main()
{
    //use the mersenne twister engine and seed it with std::random_device
    std::mt19937 rng { std::random_device()() };
    //create a uniform int distribution from 1-6 inclusive
    std::uniform_int_distribution<> dist(1,6);
    //put the engine through the distribution and print it
    std::cout << dist(rng) << std::endl;
}

Creating the engine is something you don’t want to be continuously creating it and re-seeding it. So you could make the engine a global variable or make it part of the FBullCowGame class.

1 Like

Thanks DanM, appreciate it! I can see myself needing random variables in future projects so this is definitely useful

That talk is actually rather good too.

Privacy & Terms