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 vocabulary head on, enco

Semantic Waves

In the previous post we looked at the transfer of learning from block based coding to text based languages.  Semantic waves offer a theory that help us to structure our lessons to support transfer of learning (Maton, Waite et al).  When we present concrete examples in single contexts transfer of learning is going to be weak.  We need to present multiple examples in a range of context.  This allows us to abstract out the underlaying features.  This idea of moving along a continuum between the abstract and concrete is given by the term semantic gravity.  For instance, if we talk about an algorithm in abstract terms we might say that it is a sequence of steps to solve a problem.  At this stage we have presented it as an abstract idea so has low semantic garvity.  In a lesson we might then go on and write algorithms for drawing squares.  This represents a concrete episode with high semantic gravity.  In a good lesson we might also want to give multiple examples of algorithm in different co

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