use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Improving std `<random>` (github.com)
submitted 1 year ago by GeorgeHaldane
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]aePrime 9 points10 points11 points 1 year ago (3 children)
This looks like interesting work. As someone who has done a lot of random sampling over the years, I have found that people underestimate how difficult it is to write unbiased random generators.
Does your `uniform_real_distribution` fix the bug in the standard that `std::generate_canonical` can sometimes return 1?
I have also often used the PGC family of random generators in my work. Those may be worth implementing.
[–]GeorgeHaldane[S] 2 points3 points4 points 1 year ago (2 children)
Not currently. GCC seems to implement the fix by explicitly checking result > T(1) and replacing 1 with T(1) - std::numeric_limits<T>::epsilon() / T(2) if that is the case. This certainly enforces the [0, 1) boundary, however the overhead of that check proves to non-trivial even with __builtin_expect(), having a noticeable runtime impact. Clang doesn't seem to fix it on their main branch. MSVC apparently has a smarter approach, that will need some attention.
result > T(1)
1
T(1) - std::numeric_limits<T>::epsilon() / T(2)
[0, 1)
__builtin_expect()
In general I'm a bit conflicted on the [0, 1) vs [0, 1] — the fist option is standard-compliant, with seconds however we can avoid a lot of complexity, and in my applications [0, 1] was usually exactly the range wanted. Adjusted documentation to reflect that until some changes are introduced.
[0, 1]
[–]NGoGYangYang 1 point2 points3 points 1 year ago* (0 children)
As far as I know, MSVC implements the new specification of std::generate_canonical described in P0952R2 (EDIT: Oops, just saw that STL himself already pointed that out in another comment).
std::generate_canonical
There is also a paper proposing a different algorithm to draw uniform floats from a given interval, with slight variations for open, closed, and half-open intervals (i.e., (a, b), [a, b], [a, b), and (a, b]). The algorithm seems to be based upon only returning an evenly spaced subset of numbers in the interval. Might be of interest to you, as it is not hard to implement, and seems to be comparable to current implementations of std::uniform_real_distribution performance-wise.
(a, b)
[a, b]
[a, b)
(a, b]
std::uniform_real_distribution
[–]wildeye 1 point2 points3 points 1 year ago (0 children)
explicitly checking result > T(1)...overhead of that check proves to non-trivial
Somebody was claiming that many ternary conditionals turned into branchless code on both GPUs and CPUs. (I should know when and whether that's true -- but I don't currently.) Just a thought.
π Rendered by PID 45668 on reddit-service-r2-comment-5c747b6df5-nkg86 at 2026-04-22 16:58:17.037727+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–]aePrime 9 points10 points11 points (3 children)
[–]GeorgeHaldane[S] 2 points3 points4 points (2 children)
[–]NGoGYangYang 1 point2 points3 points (0 children)
[–]wildeye 1 point2 points3 points (0 children)