W1292 Useful Randomness

From Coder Merlin
Random Bitmap
Kuntze-Konicz Fortune

Prerequisites[edit]

Research[edit]


ComingSoonIcon.png
Coming Soon

Background summary on how random numbers are generated

Background[edit]

ComingSoonIcon.png
Coming Soon

Add section on throwing dart at ¼ of square


Unit circle 3

The value of π can be calculated by:

  1. Randomly throwing "darts" at a unit circle
  2. Counting the total number of "darts", N
  3. Counting the number of "darts" that fall within the unit circle, C
  4. The ratio of the area inside the circle to the total area is C/N
  5. 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


HintIcon.png
Helpful Hint

You can run your program from within emacs with F5-r


HintIcon.png
Helpful Hint

You can find the square root of a number using the squareRoot function. This function is included in the Foundation library, so it must be imported.

For example:

import Foundation

let d = 12.0
print(d.squareRoot())


ObserveIcon.png
Observe, Ponder, and Journal Section 1

Complete your program, then answers these questions.

  1. Estimate the value of π using your program
    1. Throw 100 darts (N = 100). What result do you obtain?
    2. Throw 1000 darts (N = 1000). What result do you obtain?
  2. How is the second result different from your previous result?
  3. How large should N be to accurately estimate π to five digits?
  4. How important is it that the dart be "thrown" randomly?

Exercises[edit]

ExercisesIcon.png
Exercises
  1. 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 π. Be sure to complete this part of the exercise in your ~/Experiences/W1292 directory.
  2.  J1292  Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted.

Key Concepts[edit]

KeyConceptsIcon.png
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