席替えの数理

# クラスの人数
ns <- 2^(1:10)
# 席替え試行数
n.iter <- 10^4
# 席が変わらなかった人数の格納用行列
no.changers.mat <- matrix(0,length(ns),n.iter)
for(j in 1:length(ns)){
	n <- ns[j]
	ori <- 1:n
	no.changers <- rep(0,n.iter)
	for(i in 1:n.iter){
		tmp <- sample(ori)
		no.changers[i] <- length(which(tmp==ori))
	}
	no.changers.mat[j,] <- no.changers
}

boxplot(t(no.changers.mat))