Cryptographically strong random numbers

Marko Rauhamaa marko at pacujo.net
Fri Oct 16 13:13:37 EDT 2015


Steven D'Aprano <steve at pearwood.info>:

> Python-Dev is arguing about which of the following three functions should be
> included:
>
> randbelow(end):
>     return a random integer in the half-open interval 0...end
>     (including 0, excluding end)
>
> randint(start, end):
>     return a random integer in the closed interval start...end
>     (including both start and end)
>
> randrange([start=0,] end [, step=1]):
>     return a random integer in the half-open range(start, stop, step)
>
>
> It has been claimed that most applications of crypto random numbers
> will only need to generate them in the half-open range 0...end
> (excluding end). If you have experience with using crypto random
> numbers, do you agree? Which of the three functions would you use?

I wouldn't really ever *need* anything but randbelow(). It has the most
natural semantics for "end."

However, why not emulate the random module?

   secrets.randrange(stop)
   secrets.randrange(start, stop[, step])
   secrets.randint(a, b)

IOW, keep each function and name them (as well as the arguments) exactly
the same.


Marko



More information about the Python-list mailing list