random.SystemRandom().randint() inefficient

Cecil Westerhof Cecil at decebal.nl
Wed Jul 27 05:27:59 EDT 2022


Chris Angelico <rosuav at gmail.com> writes:

> Incidentally - if you are actually trying to select a specific item,
> you may want to consider random.choice.

Yes, I try to select a random element, but it has also to be removed.
An element should be used at most once. This is the code I use:
    # index = randbelow(len(to_try))
    index = randrange(len(to_try))
    found = permutation[to_try.pop(index)]

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


More information about the Python-list mailing list