######################################## # Coin Toss k <- 10 # Toss a fair coin 10 times. # Perform the experiment # make k=10 choice from n=2 objects, {1,2}, one at a time, # with replacement. # The following code generate a sequence of length 10, # representing the result of 10 tosses. coin.flip <- sample(2,k,replace=TRUE) # The outcome of the experiment coin.flip # number of Heads nhead <- sum(coin.flip==1) nhead # number of Tails ntail <- sum(coin.flip==2) ntail # make a barplot for the results barplot(table(coin.flip), names.arg = c("Heads","Tails"), ylab = "Frequency") ######################################## # Dice Rolling # roll two fair dice 100 times n <- 6 k <- 100 die1 <- sample(n,k,replace=TRUE) die2 <- sample(n,k,replace=TRUE) # if you want to ombine two columns of outcomes dice <- cbind(die1,die2) # the result of the experiment dice # number of times that the same numbers come up on both dice sum(die1==die2)