HackerTubewatch
Surprise me
Frame from rand() Considered Harmful Plays via YouTube's privacy-enhanced embed

rand() Considered Harmful

3 videos recommended on HN
YouTube
1 HN user recommended this talk1 month2019–2025 score 1
Years with an HN recommendation, 2007–2026

It is the standard reference for why rand() is broken in C++ and what to use instead.

Stephan T. Lavavej's GoingNative 2013 talk explaining why calling rand() and using rand() % 100 is bad in C++, and how the C++11 <random> header does the job properly.

“recommended by Stephan T. Lavavej, maintainer of Visual Studio's C++ Standard Library, in his "rand() Considered Harmful" talk” (HN, 2025)

1 recommendations · 2 citations · Talks · intermediate

3 Hacker News comments

From the HN archive; up to 20 kept per video, favoring different commenters. Recommendations first.

@camel-cdr May 2022 Recommends
on HN thread Show HN: A glib-like multi-platform C library
Read more
Whenever I see such libraries, the first thing I check out is their PRNG implementation, and this one must be the worst I've seen so far. (The rest of the library is probably quite good, I'm just talking about the PRNG part) The library has one PRNG, and it is the following: tb_spinlock_enter(&g_lock); g_value = (g_value * 10807 + 1) & 0xffffffff; tb_spinlock_leave(&g_lock); Using this is literally worse than using the rand reference implementation. Also, tb_random_range uses a biased and slow implementation with modulo. (see [link] for the proper way to do this) I might look into adding a proper PRNG implementation, but I'll recommend a few resources: A lesson of what not to do: [this video] ("rand() Considered Harmful") Some good modern PRNGs: [link] , [link] , [link] Generating random numbers in a specific range: [link]
Lesson on what not to do with rand().

Citations, criticism and jokes

@einpoklum Aug 2019 Cites it
on HN thread Dark Corners and Pitfalls of C++
Read more
> Here is a short snippet of some abstract C++ code. As you can see, that is a code from the Windows DLL I stopped reading at this point. But, ok, I didn't really. I also noticed the code was quite dated / inopportune: * Has a "X-er" class - which probably shouldn't exist in the first place. * Using raw `delete` and `new` * Using unions instead of std::variant's * Using C library calls instead of `<algorithm>` functions (memset vs std::fill) * Using `rand()` instead of the standard library's facilities (even though it's broken [this video] ) So... it's outdated and inelegant C++, regardless of any actual errors.
Why rand() is broken in C++.
@slavik81 Jun 2025 Cites it
on HN thread C++ Seeding Surprises (2015)
I used it because it was recommended by Stephan T. Lavavej, maintainer of Visual Studio's C++ Standard Library, in his "rand() Considered Harmful" talk, back when <random> was introduced. See 11m30s. [this video]
STL maintainer's talk recommending the <random> facilities.

Video list and Hacker News comment labels from HackerTube (prompted by @karpathy), released under ODC-By 1.0, built on the open-index/hacker-news archive. Hacker News data is provided by Y Combinator; comments remain the work of their authors. Video titles, channel names and thumbnails belong to their creators and are shown via YouTube. HackerTube Watch is an unofficial edition. Data export of 2026-10-31.

Keyboard shortcuts

/
Search
j k
Next / previous video
r
Surprise me
l
Like (video page)
w
Watch later (video page)
?
This help
Esc
Close