For what it's worth (was Re: [R] Permutations).

Rolf Turner rolf at
Wed Jul 14 00:37:05 CEST 2004

For what it's worth, here is a mild revision of my restr.perm()
function, which seems NOT to fall over.  I.e. it appears to
``reliably'' generate restricted permutations.

Whether these are genuinely ***random*** restricted permutations
(i.e. does each restricted permutation of 1:12 have the same
probability of being generated?) is not clear to me.  So ***DON'T
TRUST IT***!!!

restr.perm <- function ()
S <- 4:12
A <- list(1:3,4:6,7:9,10:12)
for(k in 1:4) {
	for(i in A[[k]]) {
		tmp <- union(i,S)
		tmp <- setdiff(tmp,G)
		if(length(tmp)==0) return(Recall())
		x <- if(length(tmp)==1) tmp else sample(tmp,1)
		G <- c(G,x)
		S <- setdiff(S,G)
	S <- union(S,A[[k]])
	R <- if(k < 4) A[[k+1]] else NULL
	R <- union(R,G)
	S <- setdiff(S,R)


					Rolf Turner
					rolf at

More information about the R-help mailing list