random.SystemRandom().randint() inefficient

Barry barry at barrys-emacs.org
Wed Jul 27 14:00:42 EDT 2022



> On 27 Jul 2022, at 17:09, Cecil Westerhof via Python-list <python-list at python.org> wrote:
> 
> Barry <barry at barrys-emacs.org> writes:
> 
>>>> On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list <python-list at python.org> wrote:
>>> 
>>> I need to get a random integer. At first I tried it with:
>>>   from secrets import randbelow
>>>   index = randbelow(len(to_try))
>>> 
>>> This works perfectly, but it took some time. So I thought I try:
>>>   from random  import SystemRandom
>>>   index = SystemRandom().randint(0, len(to_try) - 1)
>>> 
>>> A first indication is that the second version would take about two
>>> times as much time as the first. Is there a reason for this, or should
>>> this not be happening?
>> 
>> What is the OS that you are running on and its version?
>> If it’s linux what is the kernel version?
>> What version of python and where from?
> 
> That is always good information of-course.
> Debian 11.3
> 5.10.0-13-amd64
> 3.9.2
> 
> What do you mean with where the python version is from?

Because random number generator implementation depends on the
OS since it is SystemRandom() that you are comparing with python’s
Implementation.

Barry
> 
> -- 
> Cecil Westerhof
> Senior Software Engineer
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list