Hi, I'm in this situation: I what to generate N random numbers(integer) that are different from each other. One suggestion: tabel <- rep(NULL, N) for (i in 1:N){ temp <- as.integer(runif(1,1,max)) if(temp in tabel) { repeat (?) (temp <- as.integer(runif(i,i,max))) until (?) ((temp in tabel) ==FALSE) } else{ tabel[i] <- temp} I can't use repeat/until - don't exist.... Any suggestions? Thanks *Ragnhild* _________________________________________________________________ MSN Messenger http://www.msn.no/messenger Den korteste veien mellom deg og dine venner
See ?sample which has a default of replace = FALSE. Usage: sample(x, size, replace = FALSE, prob = NULL)> N<-10 > sample(1:(2*N),N)[1] 1 14 11 3 19 8 4 7 18 17 bob -----Original Message----- From: Ragnhild Sørum [mailto:rsoerum80@hotmail.com] Sent: Wednesday, November 12, 2003 9:26 AM To: r-help@stat.math.ethz.ch Subject: [R] repeat until function Hi, I'm in this situation: I what to generate N random numbers(integer) that are different from each other. One suggestion: tabel <- rep(NULL, N) for (i in 1:N){ temp <- as.integer(runif(1,1,max)) if(temp in tabel) { repeat (?) (temp <- as.integer(runif(i,i,max))) until (?) ((temp in tabel) ==FALSE) } else{ tabel[i] <- temp} I can't use repeat/until - don't exist.... Any suggestions? Thanks *Ragnhild* _________________________________________________________________ MSN Messenger http://www.msn.no/messenger Den korteste veien mellom deg og dine venner ______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help [[alternative HTML version deleted]]
Dear Ragnhild, You can break out of a loop (see ?break), but, if I understand correctly what you want, why not just use sample(max, N)? John At 02:26 PM 11/12/2003 +0000, Ragnhild S?rum wrote:>Hi, > >I'm in this situation: >I what to generate N random numbers(integer) that are different from each >other. >One suggestion: > >tabel <- rep(NULL, N) >for (i in 1:N){ > temp <- as.integer(runif(1,1,max)) > if(temp in tabel) { > repeat (?) (temp <- as.integer(runif(i,i,max))) > until (?) ((temp in tabel) ==FALSE) > } > else{ tabel[i] <- temp} > >I can't use repeat/until - don't exist.... >Any suggestions?----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
> I what to generate N random numbers(integer) that are different from each > other. > One suggestion: > > tabel <- rep(NULL, N) > for (i in 1:N){ > temp <- as.integer(runif(1,1,max)) > if(temp in tabel) { > repeat (?) (temp <- as.integer(runif(i,i,max))) > until (?) ((temp in tabel) ==FALSE) > } > else{ tabel[i] <- temp} > > I can't use repeat/until - don't exist....repeat ... until loops can be rewritten as while loops. The easiest way is this: # n unique random numbers between min and max sample(min:max, n) But maybe you don't want to generate the array min:max for some reason (because it is really large?). Then you can do the whole thing yourself: # another way to the same result left <- n tab <- NULL while (left > 0) { new <- as.integer(runif(left, min, max)) tab <- unique(c(tab, new)) left <- n - length(tab) } If max-min is really large and n is not much smaller than max-min you may want to generate more numbers than you actually need so unique() leaves you with more results and you don't have to iterate so often... cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany
As others already pointed out, the fast way is to use sample(). What I'd like to add is the following, which I learned from peeking at the C code underneath sample(): To draw n samples without replacement from 1:N (N>=n), you only need a loop from 1 to n that used up n random numbers. The algorithm is simple, but IMHO very clever: pop <- 1:N samp <- rep(NA, n) soFar <- N for (i in 1:n) { idx <- ceiling(runif(1) * soFar) samp[i] <- pop[idx] ## swap the soFar-th and idx-th element of pop. tmp <- pop[soFar] pop[soFar] <- pop[idx] pop[idx] <- tmp ## decrease the population by 1. soFar <- soFar - 1 } This is obviously not efficient in high-level languages like R, but in terms of algorithm, it is a lot more efficient than check-and-reject. IMHO this should be described in some book, but I have not seen any book describing it. Just my $0.02... Andy> -----Original Message----- > From: Ragnhild S?rum [mailto:rsoerum80 at hotmail.com] > Sent: Wednesday, November 12, 2003 9:26 AM > To: r-help at stat.math.ethz.ch > Subject: [R] repeat until function > > > Hi, > > I'm in this situation: > I what to generate N random numbers(integer) that are > different from each > other. > One suggestion: > > tabel <- rep(NULL, N) > for (i in 1:N){ > temp <- as.integer(runif(1,1,max)) > if(temp in tabel) { > repeat (?) (temp <- as.integer(runif(i,i,max))) > until (?) ((temp in tabel) ==FALSE) > } > else{ tabel[i] <- temp} > > I can't use repeat/until - don't exist.... > Any suggestions? > > > Thanks > *Ragnhild* > > _________________________________________________________________ > MSN Messenger http://www.msn.no/messenger Den korteste veien > mellom deg og > dine venner > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] > > Ripley (1987) Stochastic Simulation, pp.80-1 for one. I am > pretty sure it is Knuth's book, although I don't have that to > hand. I attribute it to > Moses & Oakford (1963).Thanks to Brian, Peter and Duncan for the info. And I have both Ripley (1987) and Knuth (1981), just not smart enough to look there... The shuffling algorithm is described on pp. 139-140 in Knuth's book (vol. 2, 2nd ed.), and in addition to Moses & Oakford, he also referenced R. Durstenfeld, CACM 7 (1964), 420. Best, Andy> -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > >