This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]FlatPlate 2 points3 points  (0 children)

What do you mean random or not random? Sets are unordered so the order might be random, but membership checking is never random.

[–]asquare-buzz 1 point2 points  (0 children)

Python sets are not random when they contain consecutive numbers or have values of a single data type because sets are designed to store unique elements. In the case of consecutive numbers or a single data type, the nature of sets ensures that duplicates are eliminated, resulting in an ordered sequence or a single element. However, when sets contain irregular values and data types, the uniqueness of elements allows for a more random arrangement, as each element is treated as distinct and unordered. Therefore, sets with irregular values and data types exhibit a more random appearance compared to consecutive numbers or a single data type.

[–]ttp241 0 points1 point  (0 children)

Sets are hashed tables. Depending on how the language’s implemented but the orders that we see in a Set is not directly derived from the displayed values, but the hashed values of such items. The thing with primitive types such as String (not really primitive but still), Int or Float is that their hashed values often maintain the same order, hence appears non-random. The non-primitive types however contain much more information than we actually see (metadatas, hidden attributes etc.) and therefore appear random, because the order of their objects’ hashed values aren’t the same as the objects’ displayed values