"To the extent that anyone cares about C++11's random-number facility at all, the C++ community is polarized between two views—one that likes it, and one that hates it." Isn't that almost a tautology? "People who care about X either love it or hate it".
No. You can recognize the importance of something and care about it a lot while thinking it's (eg) mediocre. Thinking a problem is important and having an extreme opinion of a solution are entirely different axes.
The C++ community is polarized about C++11's random-number facility
41–50 of 67 posts
Re: The C++ community is polarized about C++11's random-number facility
#42Why not specify the algorithm for std::rand? The specification, as far as I'm aware, gives no guarantees at all about the algorithm used. Why not just specify a good algorithm instead of adding a new function?
std::rand has global state. Specifying the algorithm doesn't fix that. And this lets you (easily) generate more than just integers in the range [0, INT_MAX]. std::rand doesn't have that level of convenience. There are all sorts of inconveniences with std::rand, and it looks like this solves the vast majority of them.
Adding other functions also doesn't fix std:rand. People use std::rand. They will continue to use std::rand, unless it's removed. And even then they'll keep using it, because implementations will add it back in for compatibility.
There's already a simple, half baked, crude random number generator. It's possible to fix it so that it provides at least acceptable randomness, and isn't complete junk. Doing this breaks nothing.
Why add another similar function with similar API issues?
Re: The C++ community is polarized about C++11's random-number facility
#43I like this proposal and I agree that randint is a bad idea.
I think there is a case to be made for doing both. The one advantage of randint is that it makes for a convenient migration path for those relying on std::rand() and friends. I honestly am a bit at a loss though in terms of understanding how this proposal makes things significantly easier for programmers. Absent this proposal, you would write: std::default_random_engine e(std::random_device{}); std::uniform_int_distr…
https://www.reddit.com/r/cpp/comments/31857s/random_number_g...
> One minor issue is that you’re using `default_random_engine`, which in some systems may be a LCG with a tiny 32-bit state. If so, you’ll have an RNG with a tiny period. That’s why Stephan recommends[1] that you explicitly use the Mersenne Twister. Of course, it might be something better, with more state.
> But what’s really wrong with it is that you use a single 32-bit integer to seed the RNG. If you’re using the Mersenne Twister, you’re using four bytes of state to try to seed 624 bytes. It’ll work, but it’s way worse than what the Python code does; Python uses 624 bytes of actual entropy rather than four bytes.
[1]: https://www.reddit.com/r/cpp/comments/31857s/random_number_g...
Re: The C++ community is polarized about C++11's random-number facility
#44Earlier quoted context omitted.
It is entirely possible to write a highly configurable interface with sane defaults. Indeed, the proposal in this article goes a long way in that direction.
Sometimes the right defaults depend on context unavailable to the low level library author. But irrespective of who provides the higher level API, the bottom line is that there needs to be an API that's most flexible when the flexibility is warranted. This is really in reference to Colin's "nothing more to add" remark.
Re: The C++ community is polarized about C++11's random-number facility
#45Earlier quoted context omitted.
I think there is a case to be made for doing both. The one advantage of randint is that it makes for a convenient migration path for those relying on std::rand() and friends. I honestly am a bit at a loss though in terms of understanding how this proposal makes things significantly easier for programmers. Absent this proposal, you would write: std::default_random_engine e(std::random_device{}); std::uniform_int_distr…
See the author's comments on very similar code here: https://www.reddit.com/r/cpp/comments/31857s/random_number_g... > One minor issue is that you’re using `default_random_engine`, which in some systems may be a LCG with a tiny 32-bit state. If so, you’ll have an RNG with a tiny period. That’s why Stephan recommends[1] that you explicitly use the Mersenne Twister. Of course, it might be something better, with more st…
Seriously, the whole point of platform defaults is that they make appropriate choices for the platform. Sure, you can define your own engine, but that's exactly what the original API gives you...
Re: The C++ community is polarized about C++11's random-number facility
#46Earlier quoted context omitted.
I think there is a case to be made for doing both. The one advantage of randint is that it makes for a convenient migration path for those relying on std::rand() and friends. I honestly am a bit at a loss though in terms of understanding how this proposal makes things significantly easier for programmers. Absent this proposal, you would write: std::default_random_engine e(std::random_device{}); std::uniform_int_distr…
The current way separates out what will be gotten back from the engine from the actual invocation. In comparison, the proposal lets me look at one line and know what will be returned: rng.uniform(1,17) I know exactly what to expect. Compare that to: uniform_dist(e); Unless the variable is named very well ("UniformDistOneToSix"?), I don't know what that line does. Not to mention, what if I want to roll a bunch of numb…
I guess if it is really a mystery, you could just always do:
std::uniform_int_distribution{1, 6}(e);
...and use some typedefs to avoid it being quite so verbose.I'm not sure I grok the bunch of numbers in a row scenario. Usually in that case I'd imagine you'd want to create a bunch of numbers with a consistent distribution, which is exactly why you have the structure you want. If not, you can always create new distributions in an ad hoc fashion (exactly why it is good that the parameters for a distribution are NOT template parameters). Instantiating distributions isn't costing you anything here. It's just a transient struct with a handful of fields... though if you use it for more than one call, it might somehow be more efficient (doubtful, but conceivable).
In practice, you pretty much always end up with something that holds the engine inside it, and you can always decorate it with methods like:
int roll_die(){ return std::uniform_int_distribution{1, 6}(e); }
...or alternatively: template
T uniform(const T begin, const T end) { return std::uniform_int_distribution{begin, end}(e); }Re: The C++ community is polarized about C++11's random-number facility
#47Earlier quoted context omitted.
std::rand has global state. Specifying the algorithm doesn't fix that. And this lets you (easily) generate more than just integers in the range [0, INT_MAX]. std::rand doesn't have that level of convenience. There are all sorts of inconveniences with std::rand, and it looks like this solves the vast majority of them.
> std::rand has global state. Specifying the algorithm doesn't fix that. Adding other functions also doesn't fix std:rand. People use std::rand. They will continue to use std::rand, unless it's removed. And even then they'll keep using it, because implementations will add it back in for compatibility. There's already a simple, half baked, crude random number generator. It's possible to fix it so that it provides at l…
C++11's random utilities and O'Neill's randutils aren't similar to std::rand at all. They're totally different beasts from std::rand.
Making std::rand a decent RNG would probably make the world a better place. I'm not disagreeing with that. But even if std::rand used a decent algorithm, it's still a suboptimal API.
Re: The C++ community is polarized about C++11's random-number facility
#48Earlier quoted context omitted.
I think there is a case to be made for doing both. The one advantage of randint is that it makes for a convenient migration path for those relying on std::rand() and friends. I honestly am a bit at a loss though in terms of understanding how this proposal makes things significantly easier for programmers. Absent this proposal, you would write: std::default_random_engine e(std::random_device{}); std::uniform_int_distr…
See the author's comments on very similar code here: https://www.reddit.com/r/cpp/comments/31857s/random_number_g... > One minor issue is that you’re using `default_random_engine`, which in some systems may be a LCG with a tiny 32-bit state. If so, you’ll have an RNG with a tiny period. That’s why Stephan recommends[1] that you explicitly use the Mersenne Twister. Of course, it might be something better, with more st…
That is super annoying.
Re: The C++ community is polarized about C++11's random-number facility
#49Features like this make me think that C++ is designed by people who think that perfection is reached when there is nothing more to add. As a C developer, I side with Antoine de Saint Exupéry.
Sigh... Too often that criticism is leveled at C++ without an understanding of the terrain. I don't think C developers should be sitting on a high horse on this one. C represents a pretty good example of the problem. Almost every program that relies upon the standard C runtime's random functions has a flawed random distribution, so any correct program completely bypasses that infrastructure and/or has a ton of additi…
Re: The C++ community is polarized about C++11's random-number facility
#50Features like this make me think that C++ is designed by people who think that perfection is reached when there is nothing more to add. As a C developer, I side with Antoine de Saint Exupéry.
Or maybe your idea of "just the right amount of stuff" is different from someone else's idea of that, and we could be thankful that the world is able to accommodate more than one opinion on this by providing us with multiple programming languages we can use according to our needs and preferences.