I want to simulate an experiment where you throw a fair dice 100 times and count the number of ones. I want to repeat this experiment 10^5 times and save the outcomes.
Here is my code to throw a dice n
times
dice <- function(n) {
sample(c(1:6),n,replace = TRUE)
}
x <- dice(100)
Next, I want to count the number of 1's and do the simulation 10^5 times (this part is wrong):
x <- numeric(10^5)
for(n in 1:10^5){
x[n] <- sum(dice(100))
}
hist(x,
main="100 Fair Rolls",
xlab="Rolls",
ylab="Probability",
xlim=c(0,100),
breaks=-1:1000+1,
prob=TRUE)
You were very close I think. If you change your for loop as follows it should work.
for(n in 1:10^5){
x[n]<-sum(dice(100)==1)
}