r <- runif(10)
print(r)
r < 0.4
# simulate 10000 flips of two unfair coins
# count the number of times that BOTH land heads
count <- 0 # count starts at zero
for(i in 1:10000){ # loop 10000 times
# generate random numbers between 0 and 1
r <- runif(1)
s <- runif(1)
# if both coins are heads, then increment counter
if(r < 0.4 && s < 0.6){
count <- count + 1
}
}
print(count) # this is the number of times both coins land heads
# more concise code to solve the same problem as above
r <- runif(10000) # 10000 flips of the first coin
s <- runif(10000) # 10000 flips of the second coin
count = (r < 0.4) & (s < 0.6)
#print(count)
print(sum(count))
## Sampling in R
dice <- sample(1:6, 3, TRUE)
print(dice)
dice == 1
# simulate the probability that at least two sixes appears in
# three rolls of a standard, fair die
c <- 0
for(i in 1:10000){
dice <- sample(1:6, 3, TRUE) # three die rolls
sixes <- sum(dice == 1) # number of ones in the rolls
if(sixes >= 2){
c <- c + 1 # increment counter
}
}
print(c/10000)
# simulate the probability that at least 18 people out of 3000 people
# have the same birthday
count <- 0
for(i in 1:10000){
bdays <- sample(1:365, 3000, TRUE)
tab <- table(bdays)
if(max(tab) >= 18){
count <- count + 1
}
}
print(count/10000)