Hi people,(adsbygoogle = window.adsbygoogle || []).push({});

I'm trying to figure out what's wrong with my code.

My goal is to calculate the average number of stones it takes so that 2 stones become adjascent on a 19x19 goban (the board of the game of go), when we randomly pick an intersection and place a stone on it. The answer should be almost 13 if I remember well.

Here's my code:

Now, when trials = 1000 like in the code, one would expect the result to be close to 13. But it returns a number around 622. When trial is worth 5 I get an output of around 17...Code (Python):

# Program that estimates the average number of stones required so that 2 stones

# become adjascent in an nxn goban. I want to plot the distribution of number

# of stones vs numbers of trial, i.e. an histogram.

import random

import numpy as np

# Size of the board

n = 19

# Number of trials

trials = 1000

# Initialize the board list of lists to 0. 0 means no stone, 1 means stone.

# Note that board[0][0] is the first element and board[20][20] is the last one

# , for when n=19. That's because I define the board

# as n+2 x n+2 for the edges...

board = [[0]*(n+2) for i in range(n+2)]

counter = 0

# Generate random coordinates inside of the 19x19 board

x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)

# Now define a boolean function that I'll use as a condition in a while loop

def check_if_stones(x_coordinates, y_coordinates):

if (board[x_coordinates-1][y_coordinates] == 1

or board[x_coordinates][y_coordinates-1] == 1

or board[x_coordinates+1][y_coordinates] == 1

or board[x_coordinates][y_coordinates+1] == 1):

return False

else:

return True

trial_list = []

for j in range(trials):

while check_if_stones(x_coordinates, y_coordinates):

if board[x_coordinates][y_coordinates] == 0:

# Now replace a random element by 1

board[x_coordinates][y_coordinates] = 1

counter += 1

x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)

x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)

# Update the counter when the boolean function returns false. Do this outside of the while loop.

counter += 1

trial_list.append(counter)

x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)

print(np.mean(trial_list))

So that np.mean(trial_list) depends heavily on "trials" while it shouldn't depend that heavily on it.

I've no idea what's wrong with the code and I'm desperate to fix it. I'd appreciate if you could spot anything weird, thanks!

P.D.:Sorry about the indentation, it seems to be broken when I paste it from Emacs. I've edited it manually but I may have goofed here on PF somewhere.

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# [Python] Trying to debug my program

Tags:

Loading...

Similar Threads - Python Trying debug | Date |
---|---|

Python How can I run Python by getting values from Matlab | Mar 7, 2018 |

Python Interesting article how to use Pandas with Excel sheets | Feb 19, 2018 |

Porting Python Code to Fortran for Parallel Computing | Dec 15, 2017 |

Trying to learn Python; is this book incorrect? | Dec 31, 2015 |

Trying to teach myself Python | Sep 17, 2012 |

**Physics Forums - The Fusion of Science and Community**