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

  • SEARCH
  • Home
  • 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 7855151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:18:01+00:00 2026-06-02T20:18:01+00:00

In R I have the following example module which repeats a for loop n

  • 0

In R I have the following example module which repeats a for loop n times:

function(n){
#inputs - n - number of results required
    #reserve n spaces for results
    r_num_successes <- 1:n

    #start looping n times
    for(i in 1:n){

        #set first uniform "random" deviate equal to 0.05 and number of successes to 0
        current_unif <- 0.05
        num_successes <- 0

        #start while loop that updates current_unif - it runs as long as 
        #current_unif is less than 0.95, increments num_successes each loop
        while(current_unif < 0.95){

            #set current_unif to a uniform random deviate between the
            #existing current_unif and 1
            current_unif <- runif(1,current_unif)
            num_successes <- num_successes + 1
        }

        #set the i-th element of the results vector to that final num_successes
        #generated by the while loop
        r_num_successes[i] <- num_successes
    }

            #output the mean of all the successes
    return(mean(r_num_successes))
}

When n gets big, this starts to grind pretty slowly. Is there a good way to optimise it?

  • 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-02T20:18:02+00:00Added an answer on June 2, 2026 at 8:18 pm

    There’s nothing you can do to significantly improve the speed of this using pure R. Byte-compiling will give you a small improvement, but you will need to move to compiled code for any significant speed gains.

    UPDATE: Here’s a Rcpp solution, just for Dirk 🙂

    > nCode <- '
    +   int N = as<int>(n);
    +   std::vector<double> rns;
    + 
    +   RNGScope scope;  // Initialize Random number generator
    + 
    +   for(int i=0; i<N; i++) {
    +     double current_unif = 0.05;
    +     double num_successes = 0;
    +     while(current_unif < 0.95) {
    +       current_unif = ::Rf_runif(current_unif, 1.0);
    +       num_successes++;
    +     }
    +     rns.push_back(num_successes);
    +   }
    + 
    +   double mean = std::accumulate(rns.begin(), rns.end(), 0.0) / rns.size();
    +   return wrap(mean);  // Return to R
    + '
    >
    > library(inline)
    > nFunRcpp <- cxxfunction(signature(n="int"), nCode, plugin="Rcpp")
    > library(compiler)
    > nFunCmp <- cmpfun(nFun)
    > system.time(nFun(1e5))
       user  system elapsed 
      3.100   0.000   3.098 
    > system.time(nFunCmp(1e5))
       user  system elapsed 
      2.120   0.000   2.114 
    > system.time(nFunRcpp(1e5))
       user  system elapsed 
      0.010   0.000   0.016 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following example file where each number is represents a byte
I have the following RSpec example which is passing: describe UsersController do it should
I have the following module, which affects how users arrive at two different content
I'm trying to figure out how XSLT process namespace prefixes and have following example:
I have the following example, compiled in VS2005, warning level 4: int main(int argc,
I have the following example of reading from a buffered reader: while ((inputLine =
I have the following example in a SQL table Cust Group Sales A 1
I have the following example of what a user might type into a field
If I have the following: /* * example.h */ #ifndef EXAMPLE #define EXAMPLE #include
Let's have a following example: public class X { } public class Y {

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.