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 ...

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...

Block to Text Programming

When we move students on from block to text programming, we want to transfer the learning that they have made on block coding into text programming.   When we are transferring learning we want to transfer knowledge from one context into another. In moving from one context to another we need to think in an analogical manner such that when we know how to solve a problem in one domain we can use it to help us solve a problem in another domain. The use of analogous examples helps learners see the deeper structure. We do this by seeking out deep structures and remove the surface structure of a problem. This approach is called expansive framing and we have expectation of future use in a different context. In contrast, bounded framing does not have utility beyond the current learning. Current learning needs to make connections to earlier contexts from which learners are expected to transfer in knowledge to the new context. Transfer of learning does not happen by itself; the teacher ...