Self.bally = r # if we hit a peg move left or right Sets the board running in a big loop for the specified number of ballsįor b in range(1, self.ballcount + 1): # set ball horizontal position to centre Pegy+= 2 # don't forget to call the function to tell the UI to draw the new board Self.prevbally = -1 # create 2D array of pegs using letter 'O' to indicate a peg Pegy = 3 # create a list for totals of the necessary size """ # need to allow for spaces between pegs and a larger space above the pegs for balls to drop Initializes the data structure to represent the Galton board Simply set the attributes of the class from init arguments or to defaults Which are called by the class whenever anything happens whichĭef _init_(self, rowcount = 7, ballcount = 40, on_init = None, on_ball_moved = None, on_total_changed = None): """ Instead 3 functions are provided as arguments to init In galtonboard.py type or paste the following code. You can download the source code as a zip or clone/download from Github if you prefer. CodingĬreate a new folder and within it create these empty source code files. The latter file can be replaced with any other providing it has a set of functions with the same parameters. When any of these things happens the specified function will be called, the Galton Board code itself having no responsilibity for or knowledge of how the visual output is handled.Īside from the file containing the main function there are two other files, one for the Galton Board code and one for the functions which draw the output. Specifically there are three things a Galton Board can do which require an update to the visual output: The code can therefore be used with any form of output by just passing the appropriate functions. These will be called when something happens requiring the graphical output to be updated. The actual mechanism I will use to do this is to pass functions to the functions controlling the board. To this end I will keep the data structures and logic completely separate from the visual output so a new output can be "plugged in". I will use the very rudimentary graphical and animation abilities of the terminal for this project, but the data structure and associated logic can be used with any environment offering graphical output such as more sophisticated terminal output using curses, or perhaps Tkinter. We also need to keep a running total of how many balls have taken each path, and finally some kind of visual display of what is happening. Implementing a software simulation of a Galton Board is straightforward: we just need a data structure representing the board and code simulating the random passage of balls through it. There are plenty of images and videos if you want to do a quick search. There also need to be containers of some kind to catch the balls from each final destination so they can be counted. It is a bit like a simple pinball machine with pegs arranged in the pattern of a Pascal's Triangle and mounted vertically to let gravity do the work of propelling balls from one peg to the next. This is all very abstract but the concept can be made physical with a contraption called a Galton Board, named after Sir Francis Galton but also called a bean machine of quincunx. Another key characteristic of Pascal's Triangle is that the probabilities build up row by row into an approximation of the normal distribution. Probabilities always add up to 1, indicating certainty - if you start at the top and keep going, whatever path you take and wherever you end up, you are certain to get to one of the numbers in the last row. The total number of paths is 16 so the total probability isĥ-Row Pascal's Triangle Total Path Probabilities So for example in the 5-row triangle above the probability of taking a particular path is (We subtract 1 from the row count as there is no choice involved on the first row, there only being one number.) This is the graphic I used in the previous post to illustrate a five-row Pascal's Triangle.Ī key characteristic of Pascal's Triangle is that if you start at the top and than trace a path downwards, randomly choosing to go left or right, each number you land on tells you how many different paths there are to that number. I recently wrote a post on Pascal's Triangle and in this post I will write a program in Python to implement a Galton Board simulator, a Galton Board being an actual physical gadget following the Pascal Triangle's probabilistic characteristics.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |