### This is a program used to simulate rolling fair dice. # Specify the number of dice to be rolled. NumberOfDice = 2 # Specify the sum you are interested in. DesiredSum = 7 # Simulate rolling dice. SumOfRandomDice = function(n){ S = 0 for (i in 1:n){S = S + trunc(6*runif(1)) + 1} return(S) } # 1. # a. Reps = 100 Count = 0 for(i in 1:Reps){ if(SumOfRandomDice(NumberOfDice) == DesiredSum) Count = Count + 1 } Prob = Count/Reps cat("The count is", Count,"for",Reps,"rolls.","\n") # \n give a cat("The probability of",DesiredSum,"for",NumberOfDice,"dice is", Prob,".\n") # b. Reps = 1000 Count = 0 for(i in 1:Reps){ if(SumOfRandomDice(NumberOfDice) == DesiredSum) Count = Count + 1 } Prob = Count/Reps cat("The count is", Count,"for",Reps,"rolls.","\n") # \n give a cat("The probability of",DesiredSum,"for",NumberOfDice,"dice is", Prob,".\n") # c. Reps = 10000 Count = 0 for(i in 1:Reps){ if(SumOfRandomDice(NumberOfDice) == DesiredSum) Count = Count + 1 } Prob = Count/Reps cat("The count is", Count,"for",Reps,"rolls.","\n") # \n give a cat("The probability of",DesiredSum,"for",NumberOfDice,"dice is", Prob,".\n") # 2. NumberOfDice = 4 DesiredSum = 14 Reps = 10000 Count = 0 for(i in 1:Reps){ if(SumOfRandomDice(NumberOfDice) == DesiredSum) Count = Count + 1 } Prob = Count/Reps cat("The count is", Count,"for",Reps,"rolls.","\n") # \n give a cat("The probability of",DesiredSum,"for",NumberOfDice,"dice is", Prob,".\n")