## Check some properties of uniform random numbers as simulated by R. ## Generate a large number of uniform random variables. nrep = 1e6 U = runif(nrep) ## Calculate the proportion of draws in each decile, e.g. [0,0.1), ## [0.1,0.2), ..., [0.9,1). M = NULL for (k in 1:10) { I = ((U >= (k-1)*0.1) & (U < k*0.1)) M[k] = sum(I) } ## A crude test of non-independence: estimate the probability that a ## value is greater than 0.9, given that the preceeding value was ## greater than 0.9. QN = 0 QD = 0 for (i in 2:nrep) { if (U[i-1] > 0.9) { QD = QD+1 if (U[i] > 0.9) { QN = QN+1 } } } p_est1 = QN/QD ## A vectorized version of the previous code. p_est2 = sum((U[2:nrep]>0.9) & (U[1:(nrep-1)]>0.9)) / sum(U[1:(nrep-1)]>0.9)