Random from Dictionary
Martin von Loewis
loewis at informatik.hu-berlin.de
Thu Oct 18 13:20:18 EDT 2001
doshea at mac.com (Tim Payne) writes:
> I've tried popitem(), but arbitrary values just aren't good enough. I
> need something that is random. Are there any other functions that I've
> missed, or will I have to create my own?
You will have to create your own. Something truly random is not
available in Python at all; Python is completely deterministic.
If you accept the pseudo-RNG of the random module, I recommend to use
the algorithm in random.shuffle on dict.keys() to establish x random
keys.
Notice that popitem() deliberately does not give a random key, since
doing so would produce a much more expensive implementation. Anybody
in need of truly random keys would have to find a source for truly
random numbers, anyway.
Regards,
Martin
More information about the Python-list
mailing list