How can I code a sample of random numbers from a list, so that each number generated follows some condition depending on the preceding number?
<p>Example: If the first number in the sample is 1, then the next number can only be  2, 4 or 5, and if the second number is 2, then the next number can only be 1, 3, 4, 5 or 6. This continues for all the numbers in the list.</p>

<p>I tried using this:</p>

<pre><code>import random
num_list = [1,2,3,4,5,6,7,8,9]
new_sample = random.sample(num_list, 5)
print (new_sample)
</code></pre>

<p>However, I can't seem to manipulate this code so that it follows the conditions I need. Any help would really be appreciated.</p>

<p>EDIT: The pattern in the conditions is a bit hard to explain. 
The numbers 1 to 9 come from a 3 x 3 grid of dots numbered in order from left to right and top to bottom. I'm trying to simulate random patterns such that you can only join adjacent dots. </p>

<p>If the preceding number (PN) = 1, then the next number (NN) = 2, 4 or 5, If PN = 2, then NN = 1, 3, 4, 5 or 6, If PN = 3 then NN = 2, 5, 6, If PN = 4 then NN = 1, 2, 5, 7, or 8, If PN = 5 then NN = 1, 2, 3, 4, 6, 7, 8, or 9 and so on. </p>

<p>I think this will work (in Python 3):   </p>

<pre><code>import random
x_arr=[]
y_arr=[]
def addTolist(m,n): 
    x_arr.append(m)
    y_arr.append(n)
grid = [[1,2,3],[4,5,6],[7,8,9]]
new_sample = random.sample(range(1,10), 5)  # to generate random list of 5 numbers
current = random.choice(new_sample)        # take random number from list of 5 numbers
for i in range(3):                         # to position of number in the grid
    for j in range(3):
        if current == grid[i][j]:
            x = i
            y = j
            break
num = [current]                        # to store the unique random numbers 
for i in range(4):                      # loop till we find the 5 th number
    print('current:',current)
    # every time empty the list 
    neighbours = []
    x_arr=[]
    y_arr=[]
    # to choose the neighbours of current number (maximum 8 neighbours)
    if y-1 &gt;= 0:
        neighbours.append(grid[x][y-1])
        addTolist(x,y-1)
        if x+1 &lt; 3:
            neighbours.append(grid[x+1][y-1])
            addTolist(x+1,y-1)
        if x-1 &gt;= 0:
            neighbours.append(grid[x-1][y-1])
            addTolist(x-1,y-1)
    if x-1 &gt;= 0:
        neighbours.append(grid[x-1][y])
        addTolist(x-1,y)
        if y+1 &lt; 3:
            neighbours.append(grid[x-1][y+1])
            addTolist(x-1,y+1)
    if y+1 &lt; 3:
        neighbours.append(grid[x][y+1])
        addTolist(x,y+1)
        if x+1 &lt; 3:
            neighbours.append(grid[x+1][y+1])
            addTolist(x+1,y+1)
    if x+1 &lt; 3:
        neighbours.append(grid[x+1][y])
        addTolist(x+1,y)
    current = random.choice(neighbours)
    while current in num:                   # till unique random number is not generted
        current = random.choice(neighbours)
    num.append(current)
    position = neighbours.index(current)
    x = x_arr[position]
    y = y_arr[position]
    print("x position:",x_arr)
    print("y position:",y_arr)
    print("neighbours:",neighbours)
print("Final Number : ",current)
</code></pre>

<p>Output:</p>

<pre><code>current: 7
x position: [1, 1, 2]
y position: [0, 1, 1]
neighbours: [4, 5, 8]
current: 4
x position: [0, 0, 1, 2, 2]
y position: [0, 1, 1, 1, 0]
neighbours: [1, 2, 5, 8, 7]
current: 5
x position: [1, 2, 0, 0, 0, 1, 2, 2]
y position: [0, 0, 0, 1, 2, 2, 2, 1]
neighbours: [4, 7, 1, 2, 3, 6, 9, 8]
current: 8
x position: [2, 1, 1, 1, 2]
y position: [0, 0, 1, 2, 2]
neighbours: [7, 4, 5, 6, 9]
Final Number :  9
</code></pre>

<blockquote>
  <p><strong>Assumption :</strong> grid is 3*3 And You have to find the 5th unique random number always.</p>
</blockquote>

<p>It is bit lengthy but I have to check all neighbours in <code>3*3</code> grid.
So I take <code>grid</code> variable which is list of list numbers in grid similar to <em>2D array</em>. I think all the remaining code is self explanatory.</p>

<p>Hope this will help you.</p>

python