# Use the method runif (random-uniform) to select # 100 numbers randomly distributed betweeen 0 and 1 my_rands<-runif(100,0,1) my_rands # get some of the statistical results on this vector mean(my_rands) sd(my_rands) median(my_rands) quantile(my_rands,0.25) quantile(my_rands,0.75) # we might use these numbers to simulate the flipping # of a coin -- heads if number > 0.5 tails otherwise coins<-ifelse(my_rands>=0.5,"H","T") coins # another way to simulate 100 coin tosses is to use the # sample method outcomes<-c("H", "T") coins2<-sample(outcomes,100, replace=TRUE) coins2 # A quick way to achieve a count of each outcome # the so-called frequencies is to use the table methd table(coins2) # the difference between replace=TRUE and replace=FALSE # can be understood if we having the program pick # lottery numbers # in PICK5 (www.palottery.state.pa.us) one picks 5 # numbers from 0-9 duplications are allowed sample(0:9, 5, replace=TRUE) # in CASH5 one picks 5 unique numbers from 1 to 43 sample(1:43, 5, replace=FALSE) # to simulate the rolling of 100 dice # and find the frequencies dice = sample(1:6, 100, replace=TRUE) table(dice) hist(dice) hist(dice, breaks=0.5:6.5)