CMSI 3630: Homework Assignment #1
Due Date: Thursday of week 02
The following guidelines are expected for all homework submissions:
- I don't care too much about what font you use or how large your margins are; however, you might
want to check out a monospaced font for typing code, as it will be easy to see the indentations.
- Speaking of indenting, PLEASE DON'T USE TABS TO INDENT YOUR CODE. Tabs can often
get interpreted differently by different computers, peripherals, and applications, and could make
code that is nicely formatted on *your* computer look
all over the map
on *my* computer or
in my editor. USE SPACES INSTEAD. You can set up almost every modern text
editor to insert spaces whenever you press the TAB key, or you can simply pound the spacebar.
- On assignments 1, 2, and 3, WORK BY YOURSELF. On
assignments 4 and 5, YOU MAY WORK IN A GROUP if you would like to do so.
However, I can't stress this enough; part of this policy is don't split up the work –
YOU MUST WORK TOGETHER on the assignment if you work together!. This
activity mimics an industry code development model called
pair programming
which is part
of the Extreme Programming software development method. Feel free to collaborate within your
group as much as you want, doing the entire assignment together.
- DO NOT share your work between individuals or groups. Doing so will count as plagiarism. If you
wish to discuss solutions with another group over coffee in the Lair, that's fine as long as it
is kept at the conceptual level and you don't share your code between
groups or individuals. Each person or group needs to turn in its own version of the solutions.
- Submit your assignment in GitHub, in your repository, to which I must be invited to be a
collaborator. I cannot evaluate what I cannot see!
- If you are working in a group you only need to turn in ONE COPY per group.
- MAKE SURE YOUR GITHUB REPO IS PRIVATE [for reasons explained on the syllabus page and in class].
- MAKE SURE TO INCLUDE ME IN YOUR REPO AS A CONTRIBUTOR so that I can upload your evaluations.
Problems for Assignment #1
Learning Outcomes: 1) make sure your Python
Development Environment is working; 2) make sure your GitHub repository is set up and accessible; 3)
make sure you can check code into your repository; 4) learn the basics of python on a simple
simulation
This assignment is actually really easy. All you need to do is:
- Get your GitHub account set up; use the naming convention
CMSI-3630_<your_name>
so I
can easily tell which one is which
- Create your repository with all the required folders; use the empty file
README.md
to start the folders [GitHub won't let you create just an empty folder ~ you need something in it]
- Make sure that the folder structure is set up the same as on the week one
page under the Assignment Submissions section
- Make sure that the repository is PRIVATE for reasons explained in class
- Invite your professor to participate in your repository; my GitHub name is
bjohnson05
and
my squirrel drinking coffee logo should be visible
- Be sure to check that your code runs from the command line, the REAL
one, not one that is part of an editor or IDE like Eclipse or VScode
- Commit one copy of your week one in-class exercise to your homework01 directory; if you are in a
group
Part Two: As Shown In Class ~ piSolver.py
OUR FIRST SIMULATION ALGORITHM! WOOHOO!
One easy way to determine the approximate value of PI is to use what's known as the Monte Carlo
simulation. To do this, implement the following steps:
- Imagine a
unit circle
which is a circle with a radius of one unit
- Inscribe that circle inside a square, so that the four sides of the square are tangent to the
circle on all four sides; this means you have a square that is 2 units by two units with the
circle inside it
- The center of the circle will be at the intersection of the diagonals of the square
- Now imagine throwing a large bunch of darts that RANDOMLY land inside the square; some will land
in the circle, some will land in the square OUTSIDE the circle
- Given the area of the square is Areasquare = 2 * 2 = 4 units
- Given the area of the circle is Areacircle = PI * r * r and since r = 1, then
Areacircle = PI units
- Taking the ratio of Areasquare to Areacircle give the equation
Areasquare / Areacircle = 4 / PI
- Rearranging that equation gives PI = (Areacircle * 4) / Areasquare
- OK, now we can create a program for this! See if you can write a program in Python that will
give an approximate value of PI using Monte Carlo Simulation. The steps are as follows:
- Generate a large number of random X and Y paired values to simulate points where
darts hit
- Count how many total points there are to simulate the area of the square — you know
this because it's the number of darts thrown
- Calculate the distance of each point from the center [origin] of the circle; if it is less
than or equal to one, that dart is INSIDE the circle so count it using a counter variable
- When all darts are thrown, use the total number thrown as Areasquare and the
total count INSIDE the circle as Areacircle
- Calculate and output the approximate value of PI using the equation developed above
- Note there are some special considerations here. For example you are counting integers, but when
you divide you will need a real number for the result. You will need real numbers for the
X and Y values, and for the distance from the origin. The distance of
each point from the origin will be the hypoteneus of a right triangle.