4 ms·
What's the modern C++ equivalent to C's (rand() % (b - a)) + a; or Python's random.randint(a, b) Easy to use and often good enough.
by sorenjan 8y ago
What's the modern C++ equivalent to C's
(rand() % (b - a)) + a;
or Python's
random.randint(a, b)
Easy to use and often good enough.
- whyever 8y ago> (rand() % (b - a)) + a; This is no longer uniform, because it introduces a bias towards small numbers.
- sorenjan 8y agoYes, it's less than ideal. But like I said, often good enough. Sometimes you just want a simple way to get something approximately random, the actual distribution might be unimportant.