Also, vectorize this puppy - that's how you really get the best performance out of R, but in this case it will easily get rid of your problem.
Michael
On Nov 19, 2011, at 11:31 PM, Peter Langfelder <peter.langfelder at gmail.com> wrote:
Well, you assign numeric(0) or numeric(0)+1, which is still
numeric(0). No wonder the return value is always numeric(0).
You probably need to replace numeric(0) simply by 0. Numeric(0) does
not mean 0, it means a numeric vector of length zero (i.e., empty).
HTH,
Peter
On Sat, Nov 19, 2011 at 6:52 PM, alex_janssen <janssena at uoguelph.ca> wrote:
Hi,
I am trying to code buffons needle in R for a class
This is my code w/ output from R, if anyone could tell me why this is
happening it would be amazing,
I can generate correct results without putting the steps into a function but
alas that is the assignment.
+ x = NULL
+ theta = NULL
+ a = NULL
+ phat = numeric(0)
+ i = 1
+
+ while ( i <= n){
+ x[i] <- runif(1,0,1/2)
+ theta[i] <- runif(1,0,pi/2)
+ a[i] <- 1/2 * (sin(theta[i])) - x[i]
+
+
+ if (a[i] <= 0)phat <- phat + 1
+ else phat<-phat
+ i <- i + 1
+
+ ?}
+ return(phat)
+ }