HackerTube
HomeLongsLikedHistoryWatch laterCategoriesPlaylistsSettings

rand() Considered Harmful

aghuttun1 HN recommender · recommended in 1 month, 2019-2025
YouTube
16K views · 13 years ago · talk · evergreen

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.

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

C, C++, Rust, Go & systems languagesProbability & game theory#c++#random#stl#c++11#goingnative

3 comments from Hacker News

@slavik812025 · in “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

CitesView on HN
@camel-cdr2022 · in “Show HN: A glib-like multi-platform C library”

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

RecommendsView on HN
@einpoklum2019 · in “Dark Corners and Pitfalls of C++”

> 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.

CitesView on HN
C++ Seasoning1:17:12

C++ Seasoning

aghuttun
58K views • 13 years ago
dotGo 2015 - Rob Pike - Simplicity is Complicated23:12

dotGo 2015 - Rob Pike - Simplicity is Complicated

dotconferences
172K views • 10 years ago
How lucky is too lucky?: The Minecraft Speedrunning Dream Controversy Explained39:47

How lucky is too lucky?: The Minecraft Speedrunning Dream Controversy Explained

Stand-up Maths
8.4M views • 5 years ago
Google I/O 2012 - Go Concurrency Patterns51:27

Google I/O 2012 - Go Concurrency Patterns

Google for Developers
894K views • 14 years ago
Going Native 2012 Keynote Stroustrup1:29:49

Going Native 2012 Keynote Stroustrup

savage309
33K views • 14 years ago
C++Now 2018: You Can Do Better than std::unordered_map: New Improvements to Hash Table Performance1:30:53

C++Now 2018: You Can Do Better than std::unordered_map: New Improvements to Hash Table Performance

CppNow
63K views • 8 years ago
Can C++ be 10x Simpler & Safer?  - Herb Sutter - CppCon 20221:54:00

Can C++ be 10x Simpler & Safer? - Herb Sutter - CppCon 2022

CppCon
277K views • 3 years ago
OSCON 2010:  Rob Pike, "Public Static Void"12:31

OSCON 2010: Rob Pike, "Public Static Void"

O'Reilly
119K views • 16 years ago
Lexical Scanning in Go - Rob Pike51:18

Lexical Scanning in Go - Rob Pike

GoogleFOSSSydney
162K views • 15 years ago
Dmitry Vyukov — Go scheduler: Implementing language with lightweight concurrency1:00:40

Dmitry Vyukov — Go scheduler: Implementing language with lightweight concurrency

Hydra
47K views • 7 years ago
Sydney Golang Meetup - Rob Pike - Go 2 Draft Specifications1:07:00

Sydney Golang Meetup - Rob Pike - Go 2 Draft Specifications

Macquarie Group
57K views • 7 years ago
Gopherfest 2015 | Go Proverbs with Rob Pike22:29

Gopherfest 2015 | Go Proverbs with Rob Pike

The Go Programming Language
317K views • 10 years ago
Rust NYC: Jon Gjengset - Demystifying unsafe code29:25

Rust NYC: Jon Gjengset - Demystifying unsafe code

Rust Programming Language
27K views • 6 years ago
De-fragmenting C++: Making Exceptions and RTTI More Affordable and Usable - Herb Sutter  CppCon 20191:33:12

De-fragmenting C++: Making Exceptions and RTTI More Affordable and Usable - Herb Sutter CppCon 2019

CppCon
90K views • 7 years ago
Rob Pike about the target group of the Go programming language (golang)0:27

Rob Pike about the target group of the Go programming language (golang)

f8C4G86p KbqEFe47
7K views • 6 years ago
CppCon 2015: Herb Sutter "Writing Good C++14... By Default"1:29:06

CppCon 2015: Herb Sutter "Writing Good C++14... By Default"

CppCon
193K views • 11 years ago
CppCon 2016: Herb Sutter “Leak-Freedom in C++... By Default.”1:39:25

CppCon 2016: Herb Sutter “Leak-Freedom in C++... By Default.”

CppCon
149K views • 10 years ago
RustConf 2020 - Closing Keynote by Siân Griffin39:03

RustConf 2020 - Closing Keynote by Siân Griffin

Rust Programming Language
10K views • 6 years ago
The Go Programming Language59:26

The Go Programming Language

Google TechTalks
406K views • 16 years ago
Abseil's Open Source Hashtables: 2 Years In - Matt Kulukundis - CppCon 201954:01

Abseil's Open Source Hashtables: 2 Years In - Matt Kulukundis - CppCon 2019

CppCon
22K views • 7 years ago