r - Need help interpreting this code -


so code handed out in our school, 1 of many examples (model of rolling fair dice).

x<-runif(1) y<-as.double(x<=c(1/6,2/6,3/6,4/6,5/6,1))*(1:6) x<-min(y[y>0])   

im having trouble understanding relation of code , rolling dice.

so first line generates 1 randomly uniform distributed number x between 0 , 1. in second line put condition x: if less 1 of components of vector (1/6,2/6,3/6,4/6,5/6,1) true=1 , else false=0.

and result multiplied vector (1,2,3,4,5,6). lastly take minimum value of vector product (has greater zero).

i cant intuition behind this. here mind explain relation of code rolling dice in real life. im confused..

so rolling dice each number has same probability of 1/6 appear.

now done here, simulate rolling dice.

therefore in first line random number between 0 , 1 generated.

the intervals compared equally sized , have length of 1/6. there for, x lie in 1 of these intervals probability again 1/6. done in third line, in interval x has fallen.

lets example: supposed x 0.25. vector of second line this:

false, true, true, true , true, true  

with multiplication get:

0, 2, 3, 4, 5, 6 

therefore @ end x equal 2.

so @ end x supposed number dice showing


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -