Skip to content
Prev 207065 / 398503 Next

about loops

Hi,

One approach could be a while loop as follows. Note that your circle
should have radius 0.5 if I understood the problem correctly.


N <- 5

npoints <- 0
ntests <- 0
points.in.circle <- matrix(NA, ncol=2, nrow=N)

while (npoints < N) {

  test.point <- runif(2, -0.5, 0.5) # generate new point in 2D
  if ( sqrt(test.point %*% test.point) <= 0.5){ # test its distance to
the origin
    npoints <- npoints + 1
    points.in.circle[npoints, ] <- test.point
  }
  ntests <- ntests + 1
}

print(paste(npoints, "out of", ntests, 'where in the circle') )

HTH,

baptiste


2010/1/21 Wolfgang?Amadeus <fdfcz123 at 163.com>: