random.SystemRandom().randint() inefficient

Cecil Westerhof Cecil at decebal.nl
Tue Jul 26 10:38:38 EDT 2022


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?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


More information about the Python-list mailing list