Triangles, Random Seeds

(Section 6.4)

Example: Chance of a Triangle

Triangle From Three Sides?

Recall the function for deciding if three side-lengths can make a triangle:

isTriangle <- function(x, y, z) {
  (x + y > z) & (x + z > y) & (y + z > x)
}

Vectorization

Recall that it worked on many triangles at once.

# sides for six triangles:
a <- c(2, 4.7, 5.2, 6, 6, 9)
b <- c(4, 1, 2.8, 6, 6, 3.5)
c <- c(5, 3.8, 12, 13, 11, 6.2)
# first triangle has sides 2, 4 and 5, etc.
isTriangle(a, b, c)
[1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE

A Random Process

  • You will take a stick that is one foot long, and
  • break it randomly in two places.
  • You will have three pieces.

Probability Question:

What is the probability that the three pieces will form a triangle?

Modeling Breaks

Make the Pieces

Do They Make a Triangle?

Repetition

runif() and isTriangle() do vectorization, so they can handle repetition. Let’s repeat the random process eight times.

Get the Sides

Ugh …

.. it is hard to interpret the results.

So let’s arrange them in a data frame:

A Helper-Function

This function does the job, straight from the original breaks:

makesTriangle <- function(x, y) {
  a <- pmin(x, y)
  b <- pmax(x, y)
  side1 <- a
  side2 <- b - a
  side3 <- 1 - b
  isTriangle(x = side1, y = side2, z = side3)
}

Apply Function

Summarise Results

How many times we got a triangle:

The proportion of times we got a triangle:

Encapsulate Our Work Into a Function

triangleSim <- function(reps = 10000 ) {
  cut1 <- runif(reps)
  cut2 <- runif(reps)
  triangle <- makesTriangle(cut1, cut2)
  cat("The proportion of triangles was ", mean(triangle), ".\n", sep = "")
}

Try It Out

Extra Feature: A Table of the Results

Add a Feature

triangleSim_table <- function(reps = 10000, table = FALSE ) {
  cut1 <- runif(reps)
  cut2 <- runif(reps)
  triangle <- makesTriangle(cut1, cut2)
  if ( table ) {
    cat("Here is a table of the results:\n")
    print(table(triangle))
    cat("\n")
  }
  cat("The proportion of triangles was ", mean(triangle), ".\n", sep = "")
}

Try It Out

Extra Feature: Option to Set the Random Seed

Random Output

When you do “random stuff”, the results are liable to be different each time.

Run this command several times:

Keeping “Random” Results the Same

What if you want to keep a record of your results?

Try setting the seed for R’s random-number generator:

Run this several times. It’s always the same color!

Another Starting Seed

You can start with a different seed:

Run this several times. It’s always the same color (but different from last time)!

Option to Set a Seed

triangleSim_table_seed <- function(reps = 10000, table = FALSE, seed = NULL) {
  if ( !is.null(seed) ) {
    set.seed(seed)
  }
  cut1 <- runif(reps)
  cut2 <- runif(reps)
  triangle <- makesTriangle(cut1, cut2)
  if ( table ) {
    cat("Here is a table of the results:\n\n")
    print(table(triangle))
    cat("\n")
  }
  cat(
    "The chance of being able to form a triangle\n",
    "is approximately ", mean(triangle), ".\n", sep = ""
  )
}

Try It Out

Try setting a seed:

Try not setting a seed:

Encapsulation and Generalization

Encapsulation and Generalization

A method for developing a computer program according to which the programmer first designs a basic procedure, then encapsulates it in one or more functions, and then progressively generalizes these functions until the program possesses all desired features.