How can Python keep generating a random list of numbers until it equals to a certain made-up list?
<p>Background: I was trying to create a code that would make Python generate a pattern from a 3 x 3 grid of dots, such that once a dot is chosen, the next dot can only be adjacent to it, and that no dot can be used twice. The following code is my input into Python for generating a 5-dot pattern for a 3 x 3 grid. </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]]        #rows of dots from top to bottom
dot = random.choice(range(1,10))        #random dot from the grid is chosen
for i in range(3):                         
    for j in range(3):
        if dot == grid[i][j]:
            x = i
            y = j 
            break
num = [dot]           
for i in range(5):                      # loop 5 times
    print('dot:',dot)
    # every time empty the list 
    neighbours = []
    x_arr=[]
    y_arr=[]
    #choosing only the adjacent numbers (neighbours) to the current dot
    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)
    dot = random.choice(neighbours)
    while dot in num:                   
        dot = random.choice(neighbours)
    num.append(dot)
    position = neighbours.index(dot)
    x = x_arr[position]
    y = y_arr[position]
</code></pre>

<p>Output:</p>

<pre><code>('dot:', 6)
('dot:', 2)
('dot:', 3)
('dot:', 5)
('dot:', 7)
</code></pre>

<p>Is there any way that I can keep generating these patterns (without repetition) until it equals to the pattern [1,2,6,9,8] and find how many trials it took to get to this pattern [1,2,6,9,8]? I assume that I would have to get the output into a single list first, but I have been struggling to do so. Any help is greatly appreciated.</p>

<p>Try this, I think it would work: </p>

<pre><code>import random
x_arr=[]
y_arr=[]
index = 0
pattern = [1,2,6,9,8]
def addTolist(m,n):
    x_arr.append(m)
    y_arr.append(n)
grid = [[1,2,3],[4,5,6],[7,8,9]]        #rows of dots from top to bottom
dot = random.choice(range(1,10))        #random dot from the grid is chosen
guess = 1
while dot!= pattern[index]:
    print("Wrong guess",dot)
    guess += 1
    dot = random.choice(range(1,10)) 
for i in range(3):                         
    for j in range(3):
        if dot == grid[i][j]:
            x = i
            y = j 
            break
num = [dot]  
print('dot:',dot)
for i in range(4):                     
    index +=1
    # every time empty the list 
    neighbours = []
    x_arr=[]
    y_arr=[]
    #choosing only the adjacent numbers (neighbours) to the current dot
    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)
    if pattern[index] not in neighbours:     # if wrong Pattern is entered
        print("Wrong Pattern can not generate",pattern[index] , "After",pattern[index-1])
        break
    dot = random.choice(neighbours)
    guess += 1 
    while dot != pattern[index]:            # for checking pattern
        print("Wrong guess",dot)
        dot = random.choice(neighbours)
        guess += 1
    print('dot:',dot)
    num.append(dot)
    position = neighbours.index(dot)
    x = x_arr[position]
    y = y_arr[position]
print("Total guess :",guess)
</code></pre>

<p>Output:</p>

<pre><code>Wrong guess 3
Wrong guess 7
Wrong guess 5
Wrong guess 8
Wrong guess 5
Wrong guess 4
dot: 1
dot: 2
Wrong guess 4
Wrong guess 1
Wrong guess 5
Wrong guess 1
Wrong guess 1
dot: 6
Wrong guess 8
Wrong guess 8
dot: 9
dot: 8
Total guess : 18
</code></pre>

<p><strong>Modifications:</strong></p>

<ul>
<li>Instead of checking unique variable , I am checking for pattern, So I
take <code>pattern</code> list.</li>
<li>I am checking for <code>range(4)</code> instead of <code>5</code> because 1st number is
already generated and so why waste computing 6th random number when
we are not even printing it.</li>
<li>I take <code>guess</code> variable to keep track of number of guesses.</li>
</ul>

<p><strong>Note :</strong> I am checking for if pattern is wrong then it will print wrong pattern .</p>

<p>For example: if pattern =<code>[1,2,3,9,8]</code>   it will give output as:</p>

<pre><code>Wrong guess 7
Wrong guess 4
dot: 1
Wrong guess 5
Wrong guess 4
Wrong guess 5
dot: 2
Wrong guess 4
dot: 3
Wrong Pattern can not generate 9 After 3
Total guess : 9
</code></pre>

<p><strong>Updated :</strong> If you want to count whole sequence as the one guess then:</p>

<pre><code>import random
x_arr=[]
y_arr=[]
index = 0
pattern = [1,2,6,9,8]
def addTolist(m,n):
    x_arr.append(m)
    y_arr.append(n)
guess = 0
while True :
    grid = [[1,2,3],[4,5,6],[7,8,9]]        #rows of dots from top to bottom
    dot = random.choice(range(1,10))        #random dot from the grid is chosen
    for i in range(3):                         
        for j in range(3):
            if dot == grid[i][j]:
                x = i
                y = j 
                break
    num = [dot]  
    for i in range(4):    
        # every time empty the list 
        neighbours = []
        x_arr=[]
        y_arr=[]
        #choosing only the adjacent numbers (neighbours) to the current dot
        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)
        dot = random.choice(neighbours)
        while dot in num :
            dot = random.choice(neighbours)
        num.append(dot)
        position = neighbours.index(dot)
        x = x_arr[position]
        y = y_arr[position]
    guess += 1
    if num == pattern :
        break
print("Total guess :",guess)
</code></pre>

<p>Output : </p>

<pre><code>Total guess : 12
</code></pre>

<blockquote>
  <p><strong>Note :</strong> I don't recommended to use this method as sometimes it gives <strong>total guess is 1</strong> or most of the times it gives you <strong>timeout</strong>
  because it is random it may happen that the required sequence may
  never gets generated or running time exceeded. SO you can try put limit on the guesses like
  instead of <code>while True</code> use <code>for c in range(50):</code> or what over range
  you have to give.(And if timeout is coming run the code more number of
  times till it gives you answer I tried 8-10 times before I got this
  output)</p>
</blockquote>

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

python