Estimating Pi
WHAT IS IT?
This program has been designed to help students gain hands-on experience working with random sampling and sample size and time. In the program, turtles draw a circle and a diameter, red and cyan respectively.
HOW TO USE IT
The user chooses how many turtles will be exploring this landscape and sets them free on randomized paths. As turtles cross either of the lines, they get captured by the line and set to the line color during a time delay. During that time, the program will count the turtles of each color and report the information on monitor windows and graphs. After that time has expired, they are released again on their ways.
THINGS TO NOTICE
The user has a broad choice of the number of turtles sampling and the amount of time that samples can be taken. In the classroom, the students would be instructed to record their data in tables and attempt to predict the eventual result. The accuracy of their results will depend on the length of time they let the program run and the number of turtles they set free.
EXPLORATIONS
Change the sample sizes and the amount of time that samples can be taken. Watch the data in tables. Can you predict the accuracy of the results?
STARLOGO FEATURES
One of our most interesting challenges was dealing with the graphical representation of the circle. Drawing the circle a number of different ways does not necessarily place a circle on the screen that results in a good approximation of Pi. It took much fiddling to draw this circle, which results in a value we thought acceptable. To find out that value, you will need to run the program many times and experiment with sample size.
Turtle procedures
;; draw-circle procedure
;; called by observer setup procedure
;; Moves the turtles 42 steps out from the center of the screen to
;; form a circle, then has each turtle stamp the patch below.
;; If the turtle has who number 0, then draw the cyan diameter line.
;;
To draw-circle
fd 42
stamp red
if who = 0 [
setc cyan
pd
bk (84)
pu
]
end
;; go procedure
;; called by the hidden Boogie button that is called by one of the sample buttons
;; Moves turtles forward with a slight wiggle then wait for a while.
;; If turtle is on a colored patch and they are the only turtle there, turn that color
;; If turtle is red or cyan, turn it back to yellow
to go
fd 1
lt (random 20) - 10
wait (5 / CountT)
if (pc = red)
[setc red wait .3]
if (pc = cyan)
[setc cyan wait .3]
if color = red
[setc yellow]
if color = cyan
[setc yellow]
end
Observer procedures
turtles-own [Ratio] ; Turtle variable
globals [
samplecount ; number of samples taken
cume ; cumulative sum of all instantaneous values of Pi
avePi ; average value for Pi
time ; time in seconds
CountT ; count of turtles in Sample size
]
;; setup procedure
;; called by thne Set up button
;; Clears all, initializes global variables, creates turtles to set up the canvas, and
;; uses these turtles to draw the circle. After the circle is drawn, the turtles are cleared.
;;
to setup
ca
setsamplecount 0
setcume 0
setavePi 0
settime 0
setCountT 0
crt 380
ask-turtles [draw-circle]
ct
end
;; clock procedure
;; called by clock button
;; Every second the time is incremented until the user hits stop button or time
;; equals the sample-time set on the slider "Sample time" by the user
;;
to clock
every 1 [
set time (time + 1)
if time = sample-time [
stopBoogie
stopoutput-databutton
stopclockbutton
]
]
end
;; start procedure
;; called by sample size buttons 50, 100, 200, 400, 800, and 1600
;; the sample size is passed in as the argument (see the instruction
;; stored in each button.)
;; Create the number of turtles passed in to do the sampling, put each at a random
;; location, and set its color to yellow. Then starts the three hidden buttons: Boogie,
;; output-data, and clock.
;;
to start :NumTurtles
set CountT :NumTurtles
crt :NumTurtles
ask-turtles [
setxy (random screen-width) (random screen-height )
setc yellow
]
startBoogie
startoutput-databutton
startclockbutton
end
;; output-data procedure
;; called by the start procedure that is called from one of the sample size buttons.
;; Outputs the following format to the output window:
;; time, instantaneous ratio of red to cyan turtles, average Pi
;;
to output-data
every 1 [
type time
type ","
let[:currcount ((count-color red) / (count-color cyan))]
type :currcount
type ","
setsamplecount samplecount + 1
setcume cume + :currcount
setavePi cume / samplecount
print avePi
]
end
to compute-avePi
output avePi
end
