Cryptographically strong random numbers

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Oct 16 13:22:15 EDT 2015


On Fri, 16 Oct 2015 18:16 Marko Rauhamaa <marko at pacujo.net> wrote:

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.



Given that the random module API won't change and is already known by many
people that would be simple to understand. Also it makes it trivially easy
to correct insecure RNG usage where needed.

--
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20151016/128dc1fd/attachment.html>


More information about the Python-list mailing list