Re: [R] Infinite loop

From: Hosack, Michael <>
Date: Tue, 22 Mar 2011 13:56:14 -0400

Thank you so much Martyn. Now I can most likely avoid having to rerun my program multiple times.


-----Original Message-----
From: Martyn Byng [] Sent: Tuesday, March 22, 2011 1:21 PM
To: Hosack, Michael
Subject: RE: [R] Infinite loop


This might do what you want:

iter <- 0
repeat {
  iter <- iter + 1
  ss <- numeric(40)
  ss[1] <- sample(1:40,1)
  for (i in 1:39) {
## calculate all possible step sizes that will give a new value in
the 1:40 range

    pmove <- sample((1 - ss[i]):(40-ss[i]))
## drop all step sizes that puts the new value within 7 places of
the previous value

    pmove <- pmove[abs(pmove)>7]
## calculate potential next values
    pss <- pmove + ss[i]
## flag any values that are already in the sample <- !(pss%in%ss)
    found <- any(
    if (found) {

      ## use the first value that is not already in the sample
      ss[i+1] <- pss[][1]
    } else {
      ## all potential values are already in the sample, so choose
another starting point

  if (found) break
  if (iter > 100) {
    cat("Giving up\n")

It randomly chooses a starting value, then chooses the next value based on a randomly selected step size rather than directly. It keeps doing this until it either uses all possible values, or gets stuck in which case it randomly selects another starting value. If it can't find a solution in 100 goes it gives up.


-----Original Message-----
From: [] On Behalf Of Hosack, Michael
Sent: 22 March 2011 15:03
Subject: [R] Infinite loop

R experts,

Hello, I am trying to sample a vector 1:40 without replacement such that no element in the new vector
is within 7 units of either of its immediate neighbors. This is part of a larger program I am working
on. The following code works well about 65 % of the time (14/40). The problem I encounter happens when
the last element remaining to be sampled from the vector STRATA is within 7 digits +- of the last element
in the vector s1, at which point an infinite loop occurs. At least that's what I think is happening.
Any help would be greatly appreciated.

Thank you,


STRATA <- 1:40
s1 <- sample(STRATA, 1)
for (i in seq(from = 1, to = 39, by = 1)){   repeat{
    tmp <- sample(STRATA, 1)

    if (!any(s1 == tmp) & !any(as.vector(IRanges(s1[length(s1)]-7, s1[length(s1)]+7)) %in% tmp)) break

  s1 <- c(s1,tmp)
s1 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.

This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}} mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 22 Mar 2011 - 18:09:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Mar 2011 - 18:10:23 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive