Skip to content

about loops

3 messages · Wolfgang Amadeus, Baptiste Auguie, David Winsemius

#
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>:
#
On Jan 21, 2010, at 7:28 AM, Wolfgang Amadeus wrote:

            
What part don't you know how to do? Sounds like homework (especially  
with the piteous appeals from a pseudonym), so we expect more evidence  
of effort on your part.

The condition for being "outside", assuming this box's lower left-hand  
corner is (0,0) is

(x-0.5)^2 + (y-0.5)^2 > 1