Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

The C++ <random> Lame List

elbeno, 7 December, 2015

Network programmers of a certain age may remember the Windows Sockets Lame List.

I previously wrote a short “don’t-do-that-do-this” guide for modern C++ randomness, and I was recently reading another Reddit exchange featuring STL, author of many parts of Microsoft’s STL implementation, when it struck me that use of C++ <random> needs its own lame list to discourage using the old and busted C parts and encourage the using the new C++ hotness. So here, in no particular order, and with apologies to Keith Moore (wherever he may be) is an incomplete lame list for use of <random>.

  1. Calling rand() or srand(). Lame.
  2. Using time(NULL) to seed an RNG. Inexcusably lame.
  3. Claiming, “But rand() is good enough for simple uses!” Dog lame.
  4. Using random_shuffle() to permute a container. Mired in a sweaty mass of lameness.
  5. Using default_random_engine. Nauseatingly lame.
  6. Using % to get a random value in a range. Lame. Lame. Lame. Lame. Lame.
  7. Not using random_device to seed an RNG. Violently lame.
  8. Assuming that random_device is going to do the right thing on your platform. Uncontrollably lame.
  9. Failing to handle a possible exception from the construction or use of random_device. Totally lame.
  10. Using anything in the standard but mt19937 or mt19937_64 as a generator. Intensely lame.
  11. Putting mt19937 on the stack. In all my years of observing lameness, I have seldom seen something this lame.
  12. Seeding mt19937 with only one 32-bit word rather than its full state_size. Pushing the lameness envelope.
  13. Forgetting that uniform_int_distribution works on a closed interval. Thrashing in a sea of lameness.
  14. Passing random_device or a generator to generate_n() by value, forgetting to wrap it with ref(). Glaringly lame.
  15. Failing to use seed_seq to initialize a generator’s state properly. Indescribably lame.
  16. Not considering thread safety when using a generator. Floundering in an endless desert of lameness.
  17. Using a global generator without making it thread_local. Suffocating in self lameness.
  18. Using RAND_MAX instead of mt19937::max(). Perilously teetering on the edge of a vast chasm of lameness.

This list will undoubtedly grow as I continue to write lame code…

C++

Post navigation

Previous post
Next post

Related Posts

VS2010 woes: tuples as map keys

20 February, 201530 June, 2015

Another day, another compiler/library bug. If you’re unfortunate enough to still be using Visual Studio 2010, don’t use tuples as map keys. #include #include #include using namespace std; typedef tuple FooT; typedef map MapT; int main(int, char*[]) { MapT m; // put a value in the map { FooT f(nullptr,…

Read More

Rules for using <random>

8 April, 20153 June, 2025

These days, it’s easy to do the right thing. Don’t do this: Don’t use std::rand(). Ever. Don’t use std::random_shuffle() to permute containers. (Too easy to misuse; can use std::rand() under the hood.) Don’t use any kind of clock for a seed. Don’t use mod (%) to get a random value…

Read More

CppCon 2015

27 September, 201527 September, 2015

CppCon 2015 is over and I’m home from Bellevue. It was a really great week; I learned a lot, talked to lots of interesting folks, and traded C++ tips and techniques with the cognoscenti. Having been to C++Now, I knew a bunch of people already, which was a good leg-up…

Read More

Comments (6)

  1. Nicola says:
    8 December, 2015 at 2:06 am

    Fun post XD

    I don’t understand, why not constructing the generator on the stack?

  2. elbeno says:
    8 December, 2015 at 8:39 am

    Because it’s large and expensive to construct. 624 32-bit words of state.

  3. Nicola says:
    8 December, 2015 at 9:12 am

    If you mean that having a 2504 byte stack frame is not a good idea, I agree.
    Raw performance is not affected, however.

  4. Adi says:
    8 December, 2015 at 12:07 pm

    The blog post itself is lame. It doesn’t say absolutely anything why any of the use cases above are bad. Could be an interesting information for those new in the area.

    The Windows Sockets lame list offered some explanations at least.

    One more note: the link http://www.elbeno.com/blog/?p=1081 seems to be down, BTW

  5. elbeno says:
    8 December, 2015 at 1:51 pm

    Re Nicola: sure, but a generator on the stack implies it’s going to be re-initialized every time through the function.

    Re Adi: I saw that the WSLL had explanations added later on, but I decided to a) keep the list a list and not a TL;DR article, b) hopefully inspire people to research and understand the why themselves.

    There’s plenty already written about this elsewhere, I just thought this was a bit of fun.

    (And the link seems fine? Must have been some transient issue.)

  6. jwp says:
    9 December, 2015 at 1:08 pm

    This list lacks the reason + alternatives that make the Winsock lame list fairly useful. Instead this article makes assumptions that the reader is already informed on the reasons why these things are lame and subsequently isn’t very helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes