title: 'Bizarre' ordering of sets in python
body: <p>When I convert a Python 3.8.0 list to a set, the resulting set ordering* is highly structured in a non-trivial way. How is this structure being extracted from the pseudo-random list?</p>
  
  <hr>
  
  <p>As part of an experiment I am running, I am generating a random set. I was surprised to see that plotting the set suddenly showed unexpected linear structure in the set. So there are two things puzzling me - why does converting to a set result have an ordering* which ends up highlighting this structure; and, to a lesser extent why does the pseudo-random set have this "hidden" structure at all?</p>
  
  <p>The code:</p>
  
  <pre class="lang-py prettyprint-override"><code>X = [randrange(250) for i in range(30)]
  print(X)
  print(set(X))
  </code></pre>
  
  <p>which outputs, for example</p>
  
  <pre class="lang-py prettyprint-override"><code>[238, 202, 245, 94, 111, 106, 148, 164, 154, 113, 128, 10, 196, 141, 69, 38, 106, 8, 40, 53, 160, 87, 85, 13, 38, 147, 204, 50, 162, 91]
  
  {128, 8, 10, 141, 13, 147, 148, 154, 160, 162, 164, 38, 40, 50, 53, 196, 69, 202, 204, 85, 87, 91, 94, 106, 238, 111, 113, 245}
  </code></pre>
  
  <p>A plot** of the above list looks fairly random, as expected:</p>
  
  <p><a href="https://i.stack.imgur.com/Vo2fW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Vo2fW.png" alt="WolframAlpha plot of randomly generated list"></a></p>
  
  <p>whereas plotting the set (as it is ordered in the output) exhibits the structure present in the set:</p>
  
  <p><a href="https://i.stack.imgur.com/ZyLxQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZyLxQ.png" alt="WolframAlpha plot of set from random list"></a></p>
  
  <p>This behaviour 100% consistent on my machine (more examples below) with the values 250 and 30 used in the above code (the example I used is not cherry picked - it is just the last one I ran). Tuning these values sometimes results in slightly different structure (e.g. a subset of three arithmetic progressions*** instead of two).</p>
  
  <p>Is this reproducible on other people's machines? Of course, that such structure exists seems indicative of a not-so-great pseudo-random number generation, but this does not explain how converting to a set would in some sense 'extract' this structure.
  As far as I am aware, the there is no formal guarantee that the ordering of a set (when converted from a list) is deterministic (and even if it is, there is no sophisticated ordering being done in the background). So how is this happening?!</p>
  
  <hr>
  
  <p>(*): I know, sets are unordered collections, but I mean "ordered" in the sense that, when calling the <code>print</code> statement, the set is output in <em>some</em> order which consistently highlights the underlying set structure.</p>
  
  <p>(**): These <a href="https://www.wolframalpha.com/input/?i=plot%20%7B128%2C%208%2C%2010%2C%20141%2C%2013%2C%20147%2C%20148%2C%20154%2C%20160%2C%20162%2C%20164%2C%2038%2C%2040%2C%2050%2C%2053%2C%20196%2C%2069%2C%20202%2C%20204%2C%2085%2C%2087%2C%2091%2C%2094%2C%20106%2C%20238%2C%20111%2C%20113%2C%20245%7D" rel="noreferrer">plots</a> are from Wolfram Alpha. Two more examples are below:</p>
  
  <p><a href="https://i.stack.imgur.com/swPSL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/swPSL.png" alt="enter image description here"></a></p>
  
  <p>(***): Two plots when changing the range of the random numbers from 250 to 500:</p>
  
  <p><a href="https://i.stack.imgur.com/b0bgM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/b0bgM.png" alt="enter image description here"></a></p>