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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:44:43+00:00 2026-06-04T17:44:43+00:00

(Rephrased the question) I’m creating a wrapper class for boost normal distribution, and want

  • 0

(Rephrased the question)

I’m creating a wrapper class for boost normal distribution, and want to make it as efficient as possible.

If I use:

double x = 0.0;
boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor(rng, nd);
for (int i=0; i<20; i++) {
     double x = var_nor();
}

The loop works fine. My concern is that I don’t want to be declaring anything unnecessarily as the method gets called many times. I tried splitting up the code and put this line in the constructor:

boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor(rng, nd);

and have a sample method that does this:

     double x = var_nor();
     return x;

But in this case, I get an error saying var_nor() (ie. with no arguments) is not found.
Can anyone tell me what’s going on with these boost declarations ie. what does the

boost:variate_generate etc.

line actually do in with var_nor?
With my limited C++ knowledge, it looks as if var_nor is being defined with two different signatures.

Thanks guys
Pete

  • 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-04T17:44:45+00:00Added an answer on June 4, 2026 at 5:44 pm

    In your code, var_nor is a variable, not a function, so it doesn’t have a signature. It represents a variate_generator object that can behave like a function because it supports operator().

    In your code, you declare and initialize var_nor at the same time. The rng and nd arguments are passed to the constructor of the variate_generator object.

    When you moved the declaration into your class’s constructor, you were declaring var_nor as a local variable in the constructor, so it’s no wonder it wasn’t available elsewhere. For something to be available throughout an entire class, it needs to be a member variable. Declare it private in the class:

    class NormalDistribution
    {
      boost::random::mt19937 _rng;
      boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor;
    public:
      NormalDistribution();
    };
    

    Then initialize it in the constructor:

    NormalDistribution::NormalDistribution():
      _rng(), var_nor(_rng, boost::normal_distribution<>(0.0, 1.0))
    { }
    

    The _rng member needs to be declared first so that it will be initialized first. The nd parameter can be omitted and replaced with a temporary normal_distribution object passed directly to the var_nor constructor as shown above.

    With those changes, you should be able to use the same normal_distribution object over multiple calls to your sample function, or whatever other uses you have for your NormalDistribution class.

    In the code you’ve since deleted from your question, you were confusing variable declarations with function declarations. You declared nd as a function receiving two parameters and returning a normal_distribution. Likewise with var_nor. It was a function when you really wanted an object. You were confused because it happens to be an object that acts like a function, but it’s still just an object.

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

Sidebar

Related Questions

My question rephrased in other words, is it bad to use <h1> with small
Similar to this question but rephrased for Linq: You can use Enumerable<T>.Any() to test
REPHRASED QUESTION: I am coming up with a list of possible image bit depth
Let me rephrase my last question, what PHP library or framework can I use
Sorry for posting this question again. I rephrased my question a little bit. I
Simple question, is it possible to simplify (or replace division or modulo by less-expensive
I'm asking this question despite having read similar but not exactly what I want
Is it possible to use 3rd party libraries written in C# in MonoTouch? For
EDIT: I rephrased the question because I have not explained well. Let's see if
EDIT: I've completed rephrased the question as I've been able to simplify my problem

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.