Exercises - Simulation in R

  1. Simulate the number of games won after playing a game 10,000 times when the probability of a win on any game played is $0.30$.

    > data=runif(10000,0,1)
    > wins=data[data < 0.30]
    > length(wins)
    [1] 3035
    

  2. Simulate a sequence of letters "R", "B", and "W" corresponding to a possible order of colors seen when randomly arranging in a sequence all of the marbles found in a bag containing $4$ red marbles, $4$ blue marbles, and $2$ white marbles, such as the one shown below.

     "W" "B" "W" "B" "R" "R" "B" "B" "R" "R"
    
      bag.of.marbles = c(rep("R",4),rep("B",4),rep("W",2))
      sample(bag.of.marbles,length(bag.of.marbles),replace=FALSE)
    
  3. Simulate a randomly drawn hand of five cards taken from a standard deck (without replacement), with a vector of strings of text similar to what is shown below, where each card is a string that indicates the rank (A, 2-10, J, Q, or K) and suit (H, D, C, or S) as the sample output below suggests.

    "9 H" "K H" "Q D" "3 D" "5 C"
    
    > suits = c("H","D","C","S")
    > ranks = c("A",paste(2:10),"J","Q","K")
    > deck = paste(rep(ranks,4),rep(suits,each=13))
    > sample(deck,5,replace=FALSE)
    
  4. Simulate with a vector like the one shown below, $10$ sums of random rolls of $4$ standard dice.

    13 14 19 22 13 15 18 13 14 19
    
      replicate(10, sum(sample(1:6, size=4, replace=TRUE))