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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:02:00+00:00 2026-06-13T03:02:00+00:00

I need to use a random number generator very often, and instead of writing

  • 0

I need to use a random number generator very often, and instead of writing it everytime I need it I would like to make a class so that I can just link it into other programs. The current code I have is this:

#include <boost/random.hpp>
#include <iostream>
int main() {
   boost::mt19937 rng(0);
   boost::normal_distribution<float> dist_normal(0.0f,1.0f);
   boost::variate_generator<boost::mt19937, boost::normal_distribution<float> > rand_normal(rng, dist_normal);

   for(int i = 0 ; i < 10; ++i)
      std::cout << rand_normal() << std::endl;
   return 0;
}

But now I would like to separate interface and implementation like so:

#include <boost/random.hpp>
#include <iostream>
int main() {
   //This goes in the header file as private members of class
   boost::mt19937 rng;
   boost::normal_distribution<float> dist_normal;
   boost::variate_generator<boost::mt19937, boost::normal_distribution<float> > rand_normal;

   //This is then in the constructor in the cpp file
   rng = boost::mt19937(0);
   dist_normal  = boost::normal_distribution<float>(0.0f,1.0f);
   rand_normal  = boost::variate_generator<boost::mt19937, boost::normal_distribution<float> >(rng, dist_normal);

   for(int i = 0 ; i < 10; ++i)
      std::cout << rand_normal() << std::endl;
   return 0;
}

But this does not compile, it gives me the following errors (oh the joy of deciphering boost errors):

test2.cpp: In function ‘int main()’:
test2.cpp:7:79: error: no matching function for call to ‘boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<float> >::variate_generator()’
test2.cpp:7:79: note: candidates are:
/usr/include/boost/random/variate_generator.hpp:69:5: note: boost::random::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, Distribution = boost::random::normal_distribution<float>]
/usr/include/boost/random/variate_generator.hpp:69:5: note:   candidate expects 2 arguments, 0 provided
/usr/include/boost/random/variate_generator.hpp:51:7: note: boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<float> >::variate_generator(const boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<float> >&)
/usr/include/boost/random/variate_generator.hpp:51:7: note:   candidate expects 1 argument, 0 provided

And here I am stuck, from what I understand the second candidate is the copy constructor, but how should I read the first candidate and how should I change my code such that it works? Thanks in advance!

EDIT:

The working solution, thanks to muehlbau.

boost_rng.c:

#include "boost_rng.hpp"

BoostRNG::BoostRNG(uint _seed) : seed(_seed), 
                                 rng(boost::mt19937(_seed)),
                                 dist_normal(boost::normal_distribution<float>(0.0f,1.0f)),
                                 rand_normal(boost::variate_generator<boost::mt19937&, boost::normal_distribution<float> >(rng, dist_normal))
{}

float BoostRNG::normal() {
    return rand_normal();
}

and boost_rng.hpp:

#include <boost/random.hpp>

class BoostRNG {
private:
    uint seed;
    boost::mt19937 rng;
    boost::normal_distribution<float> dist_normal;
    boost::variate_generator<boost::mt19937&, boost::normal_distribution<float> > rand_normal;
public:
    BoostRNG(uint seed);
    float normal();
};

Note that it works for references the the engine as well, letting me use the same engine for multiple generators 😉

  • 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-13T03:02:01+00:00Added an answer on June 13, 2026 at 3:02 am

    The problem is the following. When you declare rng, dist_normal, and rand_normal, then a boost::mt19937, a boost::normal_distribution<float>, and a boost::variate_generator<boost::mt19937, boost::normal_distribution<float> > object is constructed respectively. As far as I can tell from the error, boost::variate_generator<boost::mt19937, boost::normal_distribution<float> > does not have a default constructor and thus your compilation fails.

    You can look for solutions here: Declaring an object before initializing it in c++

    As a side note … you can always create a class, have rng, dist_normal, and rand_normal as class members and initialize them in the class constructor’s initializer list.

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

Sidebar

Related Questions

I need help on which random number generator to use simultaneously from multiple threads
I need to perform few tests where I use randn pseudo random number generator.
I use random numbers in several places and usually construct a random number generator
I need to use random class to generate random numbers in a multi threaded
I have an unsigned int storing a random number. I need to use this
I heard that computation results can be very sensitive to choice of random number
I need to be able to generate a single random number and then use
I need to get random number from Intel's random generator in processor (Intel Core
Needing a random number generator that returns a sample from a normal (Gaussian) distribution,
I need a tool which generates random JSON objects. I want to use this

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.