One of my common software interview questions involves the creation of a game board. Part of the problem includes the generation of random numbers. If the candidate is writing in C/C++, the typical code I see looks something like this:
srand(seed); // one time seeding of generator // later on ... r = (rand() % 6) + 1; // throw the dice
r = Math.Floor((Math.Random() * 5) + 1); // [1...6] ?
Takeaway: getting random numbers correct is a non-trivial problem. A lot of information available on the Internet about generating random numbers is plan wrong. Beware. Test your generators to ensure that they are both random and uniform. If I was writing C++ code today that needed to throw a d6, it would look like this:
#include <random> std::mt19937 mt(seed); // generation engine std::uniform_int_distribution<int> ud(1,6); // distribution engine // later on ... r = ud(mt); // [1...6], uniformly distributed
For a hilarious 30 min excursion into the pitfalls of C/C++ rand(), see Stephan’s talk: rand Considered Harmful. You’ll never use rand() again.