Skip to content
0:00 / 0:00

#c++#rand#random

rand() Considered Harmful

aghuttun

4.2K subscribers • 6 videos on Hallway

YouTube
  • Recommended by 1 person: 1 on Hacker News.
  • Presented by Stephan T. Lavavej

Intermediate. Assumes working knowledge.

15K views13 years ago1 recommendation

Programming languagesSystems programmingC++Algorithms

Stephan T. Lavavej explains why using rand and modulo for random numbers in C++ is problematic and how to use the C++11 random header.

Day 3 - When you need a random number, don't call rand() and especially don't say rand() % 100! This presentation will explain why that's so terrible, and how C++11's 'random' header can make your life so much easier.

Why it's here

1 person recommends this

What people say. Recommended as a classic explanation of why the traditional rand function is biased and how to properly generate random numbers in modern C++.

camel-cdron Hacker News • Recommends

On “Show HN: A glib-like multi-platform C library”

…xffffffff; 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: link to this video ("rand() Considered Harmful") Some good modern PRNGs: link , link , link Generating random numbers in a specific range: link…

u/camel-cdrin r/C_Programming • Mentions

On “A C Standard Library”

…cs. It's always preferable to run a benchmark multiple times and output the average, deviation and other useful metrics, over just outputting the time of a single run. You should also consider adding compiler specific `DoNotOptimize` macros, that help the programmer from stopping the compiler the code that is supposed to be benchmarked. (check out link to this video 3. Think about implementing a custom PRNG for `randnum_gen`, since `rand()` is quite slow and has low quality randomness on many platforms, mostly for historical reasons. Resources to check out: rand considered harmful, https://pcg-random.org, https://prng.di.unimi.it/, https://romu-random.org, and for a bit of self promo…

u/camel-cdrin r/C_Programming • Mentions

On “Bench marking Random Float Functions.”

…d()` is (on most systems) a very bad random number generator. > but you could always sample /dev/urandom for a higher entropy seed before each time you call the random function, an example provided Please, at that point, just use `/dev/urandom` directly. Now, if you are interested in the topic, I can recommend you to take a look at this video: link to this video and (a bit of self shilling) this video: link to this video (Sorry for the weird format, I didn't have that much time to spend on writing this)…

einpoklumon Hacker News • Mentions

On “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 link to this video ) So... it's outdated and inelegant C++, regardless of any actual errors.

slavik81on Hacker News • Mentions

On “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. link to this video

u/SirFletchin r/C_Programming • Mentions

On “Bench marking Random Float Functions.”

Lol I am just watching the two videos you linked now, starting with [video and that guys intro is just hilarious. Thank you for the links, you admittedly have done a much more vigilant job than me particularly in preserving a higher resolution range.

u/camel-cdrin r/C_Programming • Mentions

On “Bench marking Random Float Functions.”

`rand()` has very low quality, e.g. the output repeats quite early on windows. It's fast, but there are faster PRNGs that have very high quality and pass all randomness tests. (E.g. https://romu-random.org) You can also watch the first 20 minutes of this talk I held, which covers PRNG quality and performance. Edit: Also check out this talk link to this video "Rand considered harmful", which doesn't cover the quality of randomness that much, but shows other problems.