Skip to main content

Calculating Pi using Monte Carlo Simulations

We can use millions of random numbers to calculate Pi. This is the Monte Carlo simulation approach and is so called because it refers to the randomness inherent in casinos for which Monte Carlo is famous. This is a neat little trick and is great exercise for A level students.
We start off with a quarter circle with a radius of 1 unit. We then draw a square with an area of 1 by 1 unit that encompasses the quarter circle. We know that the area of a circle, a is Pi * r * r. Given that r=1 in this case, then a is the same as Pi. Millions of points from randomly selected pairs of x and y coordinates within the square are selected. Using Pythagoras where the magnitude of a point is given by sqrt(x*x + y*y) we can determine whether a point falls inside our outside the perimeter of the circle. The radius of the circle is 1 so if the point has a magnitude of less than 1 we know that it lies within the circle.  Based on the proportion of points that fall within the perimeter of the circle we can determine the area of the quarter circle which has an area a/4.   Given that a=Pi, then we multiply the proportion of points that fall within the perimeter of the circle by 4 and this gives us Pi. The more points we select the better our estimate of Pi but the longer simulation takes to run. The pseudocode is given here:

  1. Select random x value between 0 and 1
  2. Select random y value between 0 and 1
  3. Calculate Pythagoras: sqrt(x*x+y*y)
  4. If this value is 1 or less then it is inside the (quarter) circle otherwise it is outside.
  5. Run this millions of times and calculate the proportion of points that landed within the circle.
  6. Multiply the value by 4 and this will be Pi.
The corresponding Python code is below.

import random
import math
SAMPLES=4000000
total=0

for i in range(SAMPLES):
  x=random.random()
  y=random.random()
  z=math.sqrt(x*x+y*y)

if z<=1:
  total=total+1

print(total/SAMPLES*4)



Comments

Popular posts from this blog

Mango Learning

We are a community of teachers that have developed extensive computing resources primarily aimed at the English secondary school curriculum that can be accessed here: www.mangolearning.academy .  Mango learning empowers teachers to deliver great lessons that explain complex ideas using clear and highly scaffolded teaching and learning resources. We are very excited to offer these resources for free to the community. These teaching and learning resources for computing are made by teachers for teachers and we understand the day-to-day challenges that teacher face.   The resources incorporate general and computing specific evidence-based pedagogy. We incorporated spaced retrieval practice though knowledge organisers, diagnostic questions and quizzes, for instance. We also incorporate ideas from cognitive load theory through lots of worked examples.   To help with coding we use PRIMM and block to text based pedagogical approaches.   To support literacy we address ...

Teaching Children to Read Code using Evidence-based Approaches

Before students can write code, they need to be able to read code. Computer science pedagogy is often based around the ideas of Piaget’s constructivism - where pupils develop their knowledge through exploration, and Papert’s constructionism - where pupils learn through creating artifacts. However, evidence has shown that learners need guidance to gain useful knowledge efficiently and to organise that knowledge in a clear and logical way. They need to be able to break a problem down, remove the unnecessary detail, find patterns and think algorithmically before they can start to write programs for solving problems. Just as we wouldn’t expect a young child to write prose before they can read, we need to provide guided approaches that use direct instruction and scaffolding to help our students read code before they can be expected to write code themselves. These guided approaches are needed just as much as, if not more than, creative discovery activities. Explain the code My first approach...

Automatic Marking and Grading

While teaching is wonderful, worthwhile and rewarding it is a highly demanding and stressful profession.  So it is little surprise that there is  high rate of staff turnover with nearly o ne in 10 teachers are leaving the teaching profession in English schools each year citing burnout, overwork and stress as the principal reasons (Department for Education). To improve teacher retention a better work life balance is needed. In fact, reducing high workload was one of the motivations for the industrial action of 2023 by the NEU.  One area where large improvements can be made in work-life balance is the marking of student work. Teachers spend 9 hours per week marking student work (EEF, 2016) and if any reductions can be achieved in this area then we can go a long way to improving working conditions for teachers.  Some efforts have been made to automate grading using for instance self-marking online multiple-choice tools like  www.diagnosticquestions.com  or Mic...