I'd like to know if the absence of element ordering of the Python's built-in set
structure is "random enough". For instance, taking the iterator of a set, can it be considered a shuffled view of i开发者_如何学Gots elements?
(If it matters, I'm running Python 2.6.5 on a Windows host.)
No, it is not random. It is "arbitrarily ordered", which means that you cannot depend on it being either ordered or random.
In a word, no:
>>> list(set(range(10000))) == list(range(10000))
True
Just a note about the rigorously of the order. It seems that it is very unreliable even in the same running environment.
For example this code gives different answers:
data = 'KSRNDOW3GQ'
chars = set(data)
print(list(chars))
No, you can not rely on it for any real statistical purpose. The implementation of sets in Python is in terms of a hash table, and can cause the element distribution to display some very non-random properties. There's a large gap between "not having a guaranteed order" and "guaranteed to be unordered in a uniform-random manner".
Use random.shuffle
to really shuffle elements of a sequence.
Arbitrariness is central when designing programs, each of these freedoms that you reserve is like a joker card that you can use when you implement, develop, or rewrite your program. The more of these free-cards you collect, the more efficiency can you deliver from your code (probably), since you have more freedom to change it.
It is not random, it's only freedom. If it's a better set that way, the order can be forwards on Wednesdays and "backwards" on Fridays.
精彩评论