Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


> (rand() % (b - a)) + a;

This is no longer uniform, because it introduces a bias towards small numbers.


Yes, 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.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: