Difference between revisions of "W1292 Useful Randomness"
From Coder Merlin
Line 29: | Line 29: | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |touch main.swift}} | {{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |touch main.swift}} | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |makeSwiftMake}} | {{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |makeSwiftMake}} | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |emacs main.swift}} | |||
Line 36: | Line 37: | ||
# How many digits are you able to accurately estimate π by varying ''N''? | # How many digits are you able to accurately estimate π by varying ''N''? | ||
# How important is it that the dart be "thrown" randomly? | # How important is it that the dart be "thrown" randomly? | ||
{{Hint| | |||
You can run your program with: | |||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |run}} | |||
}} | |||
== Key Concepts == | == Key Concepts == |
Revision as of 17:10, 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)
Exercises[edit]
Create a new directory in your ~/Experiences directory named "project-1292". Then, use emacs to edit a file named "main.swift" to perform the following exercises.
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
- Estimate the value of π by performing the above experiment
- Throw 100 darts. What result do you obtain?
- Throw 1000 darts. What result do you obtain? How is this different from your previous result?
- How many digits are you able to accurately estimate π by varying N?
- How important is it that the dart be "thrown" randomly?
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