Difference between revisions of "W1292 Useful Randomness"
From Coder Merlin
Line 21: | Line 21: | ||
# The value of π is four times this value (because the area of the total square is 2 units x 2 units) | # The value of π is four times this value (because the area of the total square is 2 units x 2 units) | ||
== | == Prepare == | ||
Create a new directory in your ~/Experiences directory named "project-1292". | Create a new directory in your ~/Experiences directory named "project-1292". | ||
Use emacs to edit a file named "main.swift": | |||
{{ConsoleLine|zay-vin@codermerlin:~$ |cd ~/Experiences}} | {{ConsoleLine|zay-vin@codermerlin:~$ |cd ~/Experiences}} | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences$ |mkdir project-1292}} | {{ConsoleLine|zay-vin@codermerlin:~/Experiences$ |mkdir project-1292}} | ||
Line 32: | Line 32: | ||
# Estimate the value of π | {{Observe|Section 1| | ||
## Throw 100 darts. What result do you obtain? | Complete your program, then answers these questions. | ||
## Throw 1000 darts. What result do you obtain? How is | # Estimate the value of π using your program | ||
# How | ## Throw 100 darts (N {{Equal}} 100). What result do you obtain? | ||
## Throw 1000 darts (N {{Equal}} 1000). What result do you obtain? | |||
# How is the second result different from your previous result? | |||
# How large should N be to accurately estimate π to five digits? | |||
# How important is it that the dart be "thrown" randomly? | # How important is it that the dart be "thrown" randomly? | ||
}} | |||
== Exercises == | |||
# Write a program that randomly throws a series of N darts at a virtual dartboard as described above. At the conclusion of throwing N darts, the program should print an estimate for the value of π. | |||
# Answer all journal questions. | |||
{{Hint| | {{Hint| |
Revision as of 18:17, 5 April 2020
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Prerequisites[edit]
Research[edit]
Background[edit]
The value of π can be calculated by:
- Randomly throwing "darts" at a unit circle
- Counting the total number of "darts", N
- Counting the number of "darts" that fall within the unit circle, C
- The ratio of the area inside the circle to the total area is C/N
- The value of π is four times this value (because the area of the total square is 2 units x 2 units)
Prepare[edit]
Create a new directory in your ~/Experiences directory named "project-1292". Use emacs to edit a file named "main.swift":
zay-vin@codermerlin:~$ cd ~/Experiences
zay-vin@codermerlin:~/Experiences$ mkdir project-1292
zay-vin@codermerlin:~/Experiences$ cd project-1292
zay-vin@codermerlin:~/Experiences/project-1292$ touch main.swift
zay-vin@codermerlin:~/Experiences/project-1292$ makeSwiftMake
zay-vin@codermerlin:~/Experiences/project-1292$ emacs main.swift
Observe
Observe, Ponder, and Journal: Section 1
Complete your program, then answers these questions.
- Estimate the value of π using your program
- Throw 100 darts (N = 100). What result do you obtain?
- Throw 1000 darts (N = 1000). What result do you obtain?
- How is the second result different from your previous result?
- How large should N be to accurately estimate π to five digits?
- How important is it that the dart be "thrown" randomly?
Exercises[edit]
- Write a program that randomly throws a series of N darts at a virtual dartboard as described above. At the conclusion of throwing N darts, the program should print an estimate for the value of π.
- Answer all journal questions.
Helpful Hint
You can run your program with:
zay-vin@codermerlin:~/Experiences/project-1292$ run
Key Concepts[edit]
Key Concepts
- Random numbers meet the following two criteria:
- Even distribution over a defined interval
- Impossible to predict subsequent values based upon previous values
- Random numbers can be very useful in certain circumstances