Author vstinner
Recipients Serge Anuchin, mark.dickinson, pitrou, r.david.murray, rhettinger, serhiy.storchaka, skrah, steven.daprano, tim.peters, vstinner
Date 2015-07-10.00:13:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> Victor, if people want to use getrandbits(), we should backport the Python3 code, not reinvent it from scratch.

Sorry, I don't understand your comment. Backport what? getrandbits() is available on Python 2 and Python 3, for Random and SystemRandom.

I propose to rewrite Random.randint() in

> Your "It doesn't handle negative numbers nor empty ranges" should be a hint about what a pain it is to rewrite everything wholesale to use a _fundamentally_ different method.

In fact, I didn't check if the my code works for negative numbers. A few more lines should be enough to handle them. I just wanted to show a short pseudo-code to discuss the idea.

I don't understand your point on the pain. It's easy to replace int(i * random.random()) with randint(0, i) (sorry, I'm too lazy to check if it should be i, i-1 or i+1 :-)).

The Glib library used floats in their g_rand_int_range() method, but the function now uses only integers. The GSL library also uses integers. I'm surprised that Python uses floats to generate random numbers. For me, they are less reliable than integers, I always fear a bias caused by complex rounding and corner cases.

Do other libraries and/or programming languages use float to generate a random integer?
Date User Action Args
2015-07-10 00:13:31vstinnersetrecipients: + vstinner, tim.peters, rhettinger, mark.dickinson, pitrou, steven.daprano, r.david.murray, skrah, serhiy.storchaka, Serge Anuchin
2015-07-10 00:13:31vstinnersetmessageid: <>
2015-07-10 00:13:31vstinnerlinkissue24567 messages
2015-07-10 00:13:31vstinnercreate