Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7782269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:29:20+00:00 2026-06-01T19:29:20+00:00

i have problems to perform my R-Code in OSX. That’s my code: i <-

  • 0

i have problems to perform my R-Code in OSX.
That’s my code:

i <- 1
while (i <= 20000) {
  repeat{
    z1=((runif(1,0,1)*2)-1)
    z2=((runif(1,0,1)*2)-1)
    h=z1**2+z2**2
    if((h > 0) && (h <= 1)){break}
   }
  x[i] <- z1
  y[i] <- z2
  q[i] <- h

  i <- i + 1
} 

j <- 1
while (j <= 20000) {
  h=sqrt((-2*ln(q[j]))/q[j])
  p[j] <- h
  j <- j + 1
}

a=x*p
b=y*p
points(a,b, pch=c(20,20),col=c("dark green","red"),cex=0.6)

When I initialize x,y,q,p and use the log, it works.

But why are there those errors, but why?

error in x[i] <- z1: object 'x' not fund
error: no function for "ln" fund
error: object 'x' not fund
error: object 'y' not fund
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:29:22+00:00Added an answer on June 1, 2026 at 7:29 pm

    Here’s another approach. Your code should generally be faster if you reduce the number of times you need to do things iteratively. Specifically, all of your runif(1,0,1) calls can be replaced by one big vector of runif() values, then subset the vector based on that.

    I used @Mark Miller’s function as the starting point and made the following modifications. Note, this can be improved further if the oversampler kept the good values from the previous set of random numbers and only filled in until n was reached, but this is pretty fast regardless. For the speed comparisons, I took his code verbatim and wrapped it in fun2 <- function() {...}

    fun1 <- function(n, oversample = 1.50){
      #oversample
      over <- ceiling(n * oversample)
      goodvars <- NA
      while (length(goodvars) < n){
        z1 <- runif(over,-1,1)
        z2 <- runif(over,-1,1)
        h <- z1^2 + z2^2  
        goodvars <- which(h > 0 & h < 1)
      }
      goodvars <- goodvars[1:n]
      x <- z1[goodvars]
      y <- z2[goodvars]
      q <- h[goodvars]
      p <- sqrt((-2 * log(q)) / q)
      a <- x * p
      b <- y * p
      return(cbind(a,b))
     }
    
    ##Mark's code put into a function
    fun2 <- function() {
      i <- 1
    
      x <- rep(NA, 20)
      y <- rep(NA, 20)
      q <- rep(NA, 20)
      p <- rep(NA, 20)
    
      while (i <= 20) {
    
        repeat{
          z1=((runif(1,0,1)*2)-1)
          z2=((runif(1,0,1)*2)-1)
          h=z1**2+z2**2
          if((h > 0) & (h <= 1)){break}
        }
        x[i] <- z1
        y[i] <- z2
        q[i] <- h
    
        i <- i + 1
      } 
    
      j <- 1
      while (j <= 20) {
    
        h=sqrt((-2*log(q[j]))/q[j])
    
        p[j] <- h
    
        j <- j + 1
      }
    
      a=x*p
      b=y*p
    }
    
    #Do some speed checking with rbenchmark. Also checkout compiler package for some free speed
    library(compiler)
    library(rbenchmark)
    #Compile functions to see improvements
    cfun1 <- cmpfun(fun1)
    cfun2 <- cmpfun(fun2)
    #run benchmark tests
    benchmark(fun1(n = 20), fun2(), cfun1(n = 20), cfun2(),
              replications = 1000,
              columns=c("test", "elapsed", "relative"),
              order = "elapsed")
    

    And the results

               test elapsed  relative
    3 cfun1(n = 20)   0.042  1.000000
    1  fun1(n = 20)   0.055  1.309524
    4       cfun2()   0.407  9.690476
    2        fun2()   0.882 21.000000
    

    Starting with a new R session, copying and pasting the code above does not return an error. Here’s an example:

    test <- fun1(n = 1000)
    plot(test)
    

    enter image description here

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems trying to perform a query. I have two tables, one
I have problems with dealing with widows within a multicols environment, that is, I
I have problems getting some of my views aligned in a relative layout that
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
The following code adds tasks that perform some processing on files from the blobstore,
I have a WPF application that uses a third-party library to perform a specific
I have a function that accepts large std::vectors (passed by const & ) and
I have a piece of code that can be executed by multiple threads that
I currently have a macro that uses a form in order to perform some
I have problems binding both a telerik RadGrid and a plain vanilla ASP.NET GridView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.