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

How to support your students to write code

For many children writing code can be a daunting prospect. To help children learn to write code more easily we can use a range of scaffolded pedagogies. Initially these approaches take ownership of the code away from the students thereby giving them confidence to explore and experiment with the code.   Gradually as the students learn more and more we can reduce the amount of support until they are able to write their own programs independently.   In a previous article we looked at approaches for supporting pupils to learn to read code that included activities such as explaining, predicting and tracing code, and live demonstrations with worked examples. This follow up article presents some approaches to support pupils with writing code. Fixing broken code Children can find and fix common syntax, runtime and logical errors in a piece of code. Errors might include missing brackets, missing speech marks, spelling mistakes and missing variables declarations, for instance. Th...