Posts

  • Debunking all myths about MT19937 random number generator

    In this article I will debunk some popular myths about the Mersenne twister in C++ that have accumulated over the years. The source for some of the myths is this article named C++ Seeding Surprises. Let’s start with few definitions.

  • Backward iteration with unsigned index like size_t

    In C++11 the first tool for iterating a string or vector should be range-based for-loop. But sometimes we really need the index of the element and we need the elements in reverse order. Index is usually needed when dealing with std::string. Iterating with a signed integer is easy in both directions. But if the vector size exceeds INT_MAXyou have a logic error if you try to iterate with signed index. Thus, the most correct way is to use unsigned integer, size_t, as the type of the index. Iterating forward with size_t is same as the signed variant, but iterating backward can lead to infinite loop if you are not careful. The trick is to use the not equal comparison.

  • How to properly seed mt19937 random number generator

    There is a common myth on the Internet that seeding a Mersenne twister pseudo-random number generator with a single 32-bit number is invalid because its state contains 624 32-bit numbers. That is not true. Now I will just show the example.

subscribe via RSS