1.(a) vax亂數產生case

options("scipen"=100, "digits"=4)
vax = function(x){
  v =  ((69069*x)%%(2^32))
  v
}
lin = numeric(1000)
lin[1]=3
for(i in 2:1000){
  lin[i] = vax(lin[i-1])
}
ad = lin/2^32
ad = as.matrix(ad)
ks.test(ad,punif)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  ad
## D = 0.0189, p-value = 0.8664
## alternative hypothesis: two-sided
ad1 = hist(ad,breaks=10,plot=T)

plot of chunk unnamed-chunk-1

ad1.cou = ad1$counts
chisq.test(ad1.cou)
## 
##  Chi-squared test for given probabilities
## 
## data:  ad1.cou
## X-squared = 3.56, df = 9, p-value = 0.9379

1.(b)組合產生器case

va = function(x){
  aa=171*x%%30269
  aa
}
vb = function(y){
  bb=172*y%%30307
  bb
}
vc = function(z){
  cc=170*z%%30323
  cc
}
Andrea = function(x,y,z){
  w = ((((171*x)%%(30269))/30269)+(((172*y)%%(30307))/30307)+(((170*z)%%(30323))/30323))%%1
  w
}
Andrea(3,3,3)
## [1] 0.05079
dr = numeric(1000);xr = numeric(1000);yr = numeric(1000);zr = numeric(1000)
xr[1] = va(3) ; yr[1] = vb(3) ; zr = vc(3)
dr[1]= Andrea(3,3,3)
for(i in 2:1000){
   xr[i] = va(xr[i-1])
  yr[i] = vb(yr[i-1])
  zr[i] = vc(zr[i-1])
  dr[i] = Andrea(xr[i-1],yr[i-1],zr[i-1])
}

dr = as.matrix(dr)
dr1 = hist(dr,breaks=10,plot=T)

plot of chunk unnamed-chunk-2

dr1.cou = dr1$counts
chisq.test(dr1.cou)
## 
##  Chi-squared test for given probabilities
## 
## data:  dr1.cou
## X-squared = 19.94, df = 9, p-value = 0.01829
ks.test(dr,punif)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  dr
## D = 0.0624, p-value = 0.0008401
## alternative hypothesis: two-sided

1.(c)

casio = function(u){
   h = (3.14+u)^5%%1 
   h
}
ca = numeric(1000)
ca[1] = casio(0.3)
for(i in 2:1000){
  ca[i] = casio(ca[i-1])
}

ca1 = hist(ca,breaks=10,plot=T)

plot of chunk unnamed-chunk-3

chisq.test(ca1$counts  )
## 
##  Chi-squared test for given probabilities
## 
## data:  ca1$counts
## X-squared = 4.08, df = 9, p-value = 0.9061
ks.test(ca,punif)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  ca
## D = 0.0162, p-value = 0.9552
## alternative hypothesis: two-sided

2.(a)

len <- 10000000
fibvals <- numeric(len)
fibvals[1] <- 1
fibvals[2] <- 1
for (i in 3:len) { 
   fibvals[i] <- fibvals[i-1]+fibvals[i-2]
} 
nacci = numeric(10000000)
m=1
nacci[1:2]=runif(2)
for(n in 2:1000000){
  nacci[n+1]=(nacci[n]+nacci[n-m])%%1
}
nccu=nacci[2:10001]
nccu1=hist(nccu,breaks=100,plot=T)

plot of chunk unnamed-chunk-4

nccu1.cou = nccu1$counts
chisq.test(nccu1.cou)
## 
##  Chi-squared test for given probabilities
## 
## data:  nccu1.cou
## X-squared = 110.6, df = 99, p-value = 0.1994
ks.test(nccu,punif)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  nccu
## D = 0.0099, p-value = 0.2804
## alternative hypothesis: two-sided
####
nacc = numeric(10000000)
m=500
nacc[1:1000]=runif(1000)
for(n in 1000:1000000){
  nacc[n+1]=(nacc[n]+nacc[n-m])%%1
}
ncc=nacc[1001:11000]
ncc1=hist(ncc,breaks=100,plot=T)

plot of chunk unnamed-chunk-4

ncc1.cou = ncc1$counts
chisq.test(ncc1.cou)
## 
##  Chi-squared test for given probabilities
## 
## data:  ncc1.cou
## X-squared = 108, df = 99, p-value = 0.2511
ks.test(nccu,punif)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  nccu
## D = 0.0099, p-value = 0.2804
## alternative hypothesis: two-sided

2.(b)

3. 4. 5.(a)

un=numeric(10000)
for(i in 1:10000){
  un[i]= (sum(runif(12))-6) 
} 
un1=hist(un,breaks=100,plot=T) 

plot of chunk unnamed-chunk-6

norm = rnorm(10000)
norm11 = hist(norm,breaks=50,plot=T)

plot of chunk unnamed-chunk-6

un2 = hist(un,breaks=norm11$breaks,plot=T)

plot of chunk unnamed-chunk-6

un2.cou = un2$counts
norm.pro = norm11$counts/10000
chisq.test(un2.cou,p = norm.pro)
## Warning: Chi-squared approximation may be incorrect
## 
##  Chi-squared test for given probabilities
## 
## data:  un2.cou
## X-squared = 54.03, df = 38, p-value = 0.04423
ks.test(un,pnorm)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  un
## D = 0.0079, p-value = 0.5653
## alternative hypothesis: two-sided

5.(b)