[Python-Dev] PEP 506 secrets module

Steven D'Aprano steve at pearwood.info
Fri Oct 16 12:26:46 EDT 2015


On Fri, Oct 16, 2015 at 06:35:14PM +0300, Serhiy Storchaka wrote:

> I suggest to add only randrange(). randint() is historical artefact, we 
> shouldn't repeat this mistake in new module. The secrets module is not 
> good way to generate dice rolls. In most other cases you need to 
> generate integers in half-open interval [0; N).
> 
> And randbelow() is absolute redundant. Random._randbelow() is 
> implementation detail and I inclined to get rid of it (implementing 
> randrange() in C instead).

This was discussed on Python-Ideas, and there was little consensus there 
either. (Looks like Tim Peters' prediction is coming true :-)

Putting aside your inflammatory description of randint() as a "mistake", 
if you are correct that in most cases people will need to generate 
integers in the half-open interval [0...n) then we should keep 
randbelow, since that is precisely what it does.

randrange([start=0,] end [, step=1]) is a complex API. It can take one, 
two or three arguments, like range. Are there any use-cases for 
providing the step argument? If not, then why offer such a complex API 
that will never be used?

Personally, I have no sense of which of the three functions will be most 
useful, but if you are right about the half-open [0...n) interval, then 
randbelow seems to be the right API to offer. But I have seen people 
argue in favour of randint, and others argue in favour of randrange. 
Given that these are just thin wrappers or aliases to methods of 
random.SystemRandom, I don't think there is any harm in providing all 
three.

I've also raised this issue on the python-list mailing list.


-- 
Steve


More information about the Python-Dev mailing list