I'm trying to understand the classes in <random>
better but I'm unsure between all the different options kind of generator would work best for something like a d20 type rolling. I also understand that rand()
is considered now to be bad practice compared to the c++11 options.
Asked
Active
Viewed 106 times
0
-
https://en.cppreference.com/w/cpp/numeric/random – Jesper Juhl Apr 03 '20 at 17:41
-
1That said, for just dice rolling, you really don't need anything special. – NathanOliver Apr 03 '20 at 17:42
-
You may be interested in this as well: [Probability and Games: Damage Rolls](https://www.redblobgames.com/articles/probability/damage-rolls.html) – Jesper Juhl Apr 03 '20 at 17:49
-
@selbie That's not how you call `rand`. [`rand`](https://en.cppreference.com/w/cpp/numeric/random/rand) doesn't take any parameters. – NathanOliver Apr 03 '20 at 17:50
-
@selbie `rand()` is usually implemented as a linear congruential generator, which has pretty poor period and statistical properties. Also, the seed you provide to `srand` has 1s resolution (and is easily guessible). Two games started within the same second would use the same seed and get the same numbers. *I* wouldn't go that route. – Jesper Juhl Apr 03 '20 at 17:54
-
Even `rand()` is probably wildly better than _actual_ dice rolling, which isn't random at all but just based on a few variables like the die's position in your hand and how far you chuck it and at what angle. And onto what, I suppose. We treat this as the quintessential example of "randomness" because we're not great at measuring those things in our head (and thus at predicting the outcome), but speaking mathematically it's really no such thing. – Asteroids With Wings Apr 03 '20 at 17:58
-
A good talk to watch: [rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – Jesper Juhl Apr 03 '20 at 18:06
-
@NathanOliver - correct. Wrote it in a hurry. – selbie Apr 03 '20 at 19:09
1 Answers
1
A std::mt19937 is usually a fine choice. It has good statistical properties and it is quite fast.
Combine it with a std::uniform_int_distribution and you should be golden.
Do remember to seed it properly.
Don't use std::default_random_engine
, since you don't know what you'll get - and the minimum requirements are rather bad.
And please don't resort to srand
/rand
.

Jesper Juhl
- 30,449
- 3
- 47
- 70
-
-
@Ayxan "*implementation-defined*". Which can be *anything*. In *theory*, a constant string of zeros is OK, as long as it's documented. IIRC, `mingw` on Windows did something like that. Or maybe that was for `std::random_device` (which is also *horribly unspecified*), I don't remember. – Jesper Juhl Apr 03 '20 at 18:15
-
@JesperJuhl That was `std::random_device` back around gcc v4 or 5 with mingw IIRC. – super Apr 03 '20 at 18:38
-
-
@JesperJuhl [cppref](https://en.cppreference.com/w/cpp/numeric/random/random_device) Notes Section: *A notable implementation where std::random_device is deterministic is old versions of MinGW (bug 338, fixed since GCC 9.2), although replacement implementations exist, such as mingw-std-random_device.* – Aykhan Hagverdili Apr 03 '20 at 18:45