Question
The following figure depicts Galton's box, a game in which marbles are dropped through N rows of pins. In row 0, there is one position
The following figure depicts Galton's box, a game in which marbles are dropped through N rows of pins. In row 0, there is one position a marble can be in (labeled 0), in row 1, there are two positions (labeled 0 and 1), and so forth. Each time the marble bounces from one row to the next, there is a 50% probability it bounces left and a 50% probability it bounces right.
Notice that if a marble is in position x of row y, and it bounces left, it ends up in position x of row y+1. If it bounces right, it ends up in position x+1.
1. Create a class, Marble, to represent a single Marble that will drop through Galton's Box.
- Include attributes to represent the position of the marble.
- The __init__ method should accept a one-character label for use when printing the Marble.
2. Create a class, GaltonBox, to represent the overall setup. You should include the following methods:
-
__init__ - Your initializer should accept the size of the box (number of rows including the start row), N.
-
insert_marble - This method should accept a Marble instance and sets its position to position 0, row 0.
-
time_step - This method should cause all Marbles in Galton's box to bounce to the next row, dropping left or right with equal probability. When a marble reaches row N-1 at the bottom of the box, it should not move any more. Note that you should simply allow marbles to occupy the same position (instead of working out a system to prevent a Marble from entering a position if another Marble is already there).
- You can import the random library to decide which way the marble bounces: https://docs.python.org/3.7/library/random.htm
- If you import random, please import it at the top of your block instead of inside the class / methods.
-
__str__ and __repr__ - Include methods to display the Marbles currently in the box. To keep things simple, if there are multiple Marbles in a given position, you only have to display one of the labels.
Your classes should mimic the following behavior (except that the horizontal positions are random):
>>> m1 = Marble("x") >>> m2 = Marble("o") >>> box = GaltonBox(3) >>> box.insert_marble(m1) >>> box x -- --- >>> box.time_step() >>> box - -x --- >>> box.insert_marble(m2) >>> box o -x --- >>> box.time_step() >>> box - o- -x- >>> box.time_step() >>> box - -- ox
Once your code is working, write code to create a box with 20 rows, insert a 100 Marbles, and repeatedly call time_step() until all Marbles are at the bottom. Now adapt the following code to display a histogram of the final Marble positions. What does the shape of the distribution look like?
Transcribed image textStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started