2 ms·
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
by com2kid 11y ago
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 numbers in a row? Do I create all the different distributions that I may ever need ahead of time? If I don't know what all random ranges I'll need, I guess I can just create them on the stack as needed, instantiating objects willy nilly. Eew.
- cbsmith 11y agoI'd just call it "from_one_to_six". I guess if it is really a mystery, you could just always do: std::uniform_int_distribution<int>{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<int>{1, 6}(e); } ...or alternatively: template <typename T> T uniform(const T begin, const T end) { return std::uniform_int_distribution<T>{begin, end}(e); }