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?
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.
My data is -1,+1 randomly distributed in the lattice:
lattice=np.random.uniform(low=0.0, high=1.0, size=[30,30])
lattice[lattice<0.2]=-1
lattice[lattice>0.2]=1
So say the random element is lattice[0,0], then I'm expecting the returned lattice to be
lattice[0,0]=np.random.choice([lattice[0,1], lattice[1,0]])
edit: I'm sorry for the previous misleading use of the plural.