On Tue, 26 Jul 2022 16:38:38 +0200, Cecil Westerhof <ce...@decebal.nl>
declaimed the following:

>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?

        Well, off the top of my head...

        For one generation of "index" you are first creating an instance of
SystemRandom(), using it to generate your random integer, and then
disposing of the instance.

        If you only need ONE random integer, the time difference probably
doesn't matter. OTOH, if you need many during the run, using

        sr = SystemRandom()
        #stuff in some loop that generates multiple ints
                index = sr.randint(...)

        Hmmm, wonder if there is a speed difference between
                .randint(0, len(to_try) - 1)
and
                .randint(1, len(to_try)) - 1


-- 
        Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfr...@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to