CMSI 3630: Homework Assignment #1
Due Date: Thursday of week 02
The following guidelines are expected for all homework submissions:
- All homework must be typed. Homework which is not typed will be returned ungraded and will be
subject to the late homework guidelines as set out on the syllabus page. PLEASE DO NOT scribble
something on lined paper and rip it out of your spiral notebook — hanging chads went out
with the presidential election in 2000.
- 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, WORK WITH A PARTNER. I can't stress this enough; part of this policy
is don't split up the work – WORK TOGETHER on the assignment. 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 in your pairs 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.
- 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.
- Submit your homework through GitHub, in your repository, to which I must be a contributor or have
otherwise been allowed access. I cannot evaluate what I cannot see!
Problems for Assignment #1
Learning Outcomes: 1) make sure your Python
Development Environment is working; 2) make sure your GitHub repo is set up and accessible; 3) make sure
you can check code into your repo; 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
Part Two: As Shown In Class ~ piSolver.py
OUR FIRST SIMULATION ALGORITHM!
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
- Inscribe that circle inside a square, so that the four sides of the square are tangent to the circle
on four sides; this means you have a square that is 2 units by two units with the circle inside it
- 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
- 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
- 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