random.SystemRandom().randint() inefficient

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Jul 26 18:58:37 EDT 2022


On Tue, 26 Jul 2022 16:38:38 +0200, Cecil Westerhof <Cecil at 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
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/


More information about the Python-list mailing list