[R] Simulating distribution of max of two die

Jaap van Wyk J.vanWyk at ru.ac.za
Tue Aug 30 05:56:56 CEST 2011


Hallo

I am teaching a very elementary intro course about R, and want to show  
the students how to find the distribution of the maximum of rolling  
two balanced die. Is there perhaps a more elegant way to do this,  
other than the way I am using below? (I would like to show them two  
ways - the one shown here, and perhaps and improved/elegant approach  
(showing off the "beauty" of R.
My code is as follows:

R> n <- 20000
R> d1 <- sample(1:6, n, replace=TRUE)
R> d2 <- sample(1:6, n, replace=TRUE)
R> d <- apply(matrix(c(d1,d2), nrow=n, byrow=TRUE), 1, max)
R> round(table(d)/n, 3)
d
     1     2     3     4     5     6
0.030 0.084 0.137 0.195 0.246 0.308



More information about the R-help mailing list