title: Selecting an adjacent element from a numpy array
body: <p>I'm representing an upright square lattice with a numpy array (one element is connected to the one above, one below, to the right and to the left, but not diagonally). I need to select a random element from the lattice and change it's value to a value of a random adjacent element. Is there a simple way to do this?</p>
  
  <p>At the moment my best idea is to randomize from the row and column index and then try if the element has four neighbors, then try if the left is missing, then right, then top then bottom and finally to try the four corners. This seems really cumbersome.</p>
  
  <p>My data is -1,+1 randomly distributed in the lattice:</p>
  
  <pre><code>lattice=np.random.uniform(low=0.0, high=1.0, size=[30,30])
  lattice[lattice&lt;0.2]=-1
  lattice[lattice&gt;0.2]=1
  </code></pre>
  
  <p>So say the random element is <code>lattice[0,0]</code>, then I'm expecting the returned lattice to be</p>
  
  <pre><code>lattice[0,0]=np.random.choice([lattice[0,1], lattice[1,0]])
  </code></pre>
  
  <p>edit: I'm sorry for the previous misleading use of the plural.</p>