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 806735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:17:31+00:00 2026-05-15T00:17:31+00:00

Hey, id like to make this as fast as possible because it gets called

  • 0

Hey, id like to make this as fast as possible because it gets called A LOT in a program i’m writing, so is there any faster way to initialize a C++ vector to random values than:

double range;//set to the range of a particular function i want to evaluate.
std::vector<double> x(30, 0.0);
for (int i=0;i<x.size();i++) {
    x.at(i) = (rand()/(double)RAND_MAX)*range;
}

EDIT:Fixed x’s initializer.

  • 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-05-15T00:17:32+00:00Added an answer on May 15, 2026 at 12:17 am

    Right now, this should be really fast since the loop won’t execute.

    Personally, I’d probably use something like this:

    struct gen_rand { 
        double range;
    public:
        gen_rand(double r=1.0) : range(r) {}
        double operator()() { 
            return (rand()/(double)RAND_MAX) * range;
        }
    };
    
    std::vector<double> x(num_items);
    std::generate_n(x.begin(), num_items, gen_rand());
    

    Edit: It’s purely a micro-optimization that might make no difference at all, but you might consider rearranging the computation to get something like:

    struct gen_rand { 
        double factor;
    public:
        gen_rand(double r=1.0) : factor(range/RAND_MAX) {}
        double operator()() { 
            return rand() * factor;
        }
    };
    

    Of course, there’s a really good chance the compiler will already do this (or something equivalent) but it won’t hurt to try it anyway (though it’s really only likely to help with optimization turned off).

    Edit2: “sbi” (as is usually the case) is right: you might gain a bit by initially reserving space, then using an insert iterator to put the data into place:

    std::vector<double> x;
    x.reserve(num_items);
    std::generate_n(std::back_inserter(x), num_items, gen_rand());
    

    As before, we’re into such microscopic optimization, I’m not at all sure I’d really expect to see a difference at all. In particular, since this is all done with templates, there’s a pretty good chance most (if not all) the code will be generated inline. In that case, the optimizer is likely to notice that the initial data all gets overwritten, and skip initializing it.

    In the end, however, nearly the only part that’s really likely to make a significant difference is getting rid of the .at(i). The others might, but with optimizations turned on, I wouldn’t really expect them to.

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

Sidebar

Related Questions

hey guys im working on a task to make my story's links like this
Hey guys my current menu is like this: I need to make it so
Hey all, I would like to make possible to share links of my website
Hey, is there any built in functions or something like that in php that
Hey I would like to know if there is a way to only take
Hey i wanna do something like this: i=2; _root.(process + i)._x = var_Process_Pos_x; how
Hey. I would like to upload a file and then parse it. Because parsing
Hey, I have some text that is formatted like this: [quote]foo text[/quote] and I
Hey guys this should be simple, I'm just not seeing it, I would like
For this code i need to take a string like <b>hey</b> more text<b>hey2</b> other

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.