Difference between revisions of "W1292 Useful Randomness"
From Coder Merlin
Line 35: | Line 35: | ||
== Prepare == | == Prepare == | ||
Create a new directory in your ~/Experiences directory named " | Create a new directory in your ~/Experiences directory named "W1292". | ||
Use emacs to edit a file named "main.swift": | 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 | {{ConsoleLine|zay-vin@codermerlin:~/Experiences$ |mkdir W1292}} | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences$ |cd | {{ConsoleLine|zay-vin@codermerlin:~/Experiences$ |cd W1292}} | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ | | {{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |swift-init}} | ||
{{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |emacs main.swift}} | {{ConsoleLine|zay-vin@codermerlin:~/Experiences/project-1292$ |emacs main.swift}} | ||
{{Hint| | {{Hint| | ||
You can run your program with | You can run your program from within emacs with {{Key|F5}}-{{Key|r}} | ||
{{ | |||
}} | }} | ||
Revision as of 22:59, 19 January 2021
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Prerequisites[edit]
Research[edit]
- Random Number Generation (Wikipedia)
- How Random is Your Randomness?
- The Search for π
- Random Function for Int (Swift Documentation)
- Random Function for Double (Swift Documentation)
Coming Soon | |
Background summary on how random numbers are generated |
Background[edit]
Coming Soon | |
Add section on throwing dart at ¼ of square |
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 "W1292". Use emacs to edit a file named "main.swift":
zay-vin@codermerlin:~$ cd ~/Experiences
zay-vin@codermerlin:~/Experiences$ mkdir W1292
zay-vin@codermerlin:~/Experiences$ cd W1292
zay-vin@codermerlin:~/Experiences/project-1292$ swift-init
zay-vin@codermerlin:~/Experiences/project-1292$ emacs main.swift
Helpful Hint
You can run your program from within emacs with F5-r
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]
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