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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:54:33+00:00 2026-06-17T02:54:33+00:00

I am using a uniform_int_distribution in Boost 1.52 to generate random numbers using the

  • 0

I am using a uniform_int_distribution in Boost 1.52 to generate random numbers using the basic boilerplate code:

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

boost::random::mt19937 gen;

int roll_die()
{
    boost::random::uniform_int_distribution<> dist(1, 6);
    return dist(gen);
}

int main()
{
    for (int i = 0; i < 10; i++) std::cout << roll_die() << std::endl;     
}

I implemented parts of this in a much larger project and it works great. Here is my question.

In the above function, it seems like the dist object is local to the function. If you call roll_die() many many times, it seems like having dist() be local to the function would introduce a lot of overhead.

I’m thinking it would be better to set the min and max parameters of this object once, and then only have one instance of dist in a bigger object or something. How does one do this? I tried to understand the “Public Member Functions” portion of the class template: http://www.boost.org/doc/libs/1_47_0/doc/html/boost/random/uniform_int_distribution.html#id744736-bb but it was over my head. In that documentation I see:

void param(const param_type & param); //Sets the parameters of the distribution.

How do you actually use this? Is .param() itself a function to call, or is it a stand-in for another function? I couldn’t find another boost example that did what I’m asking. Thanks in advance for your assistance and advice!

  • 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-17T02:54:34+00:00Added an answer on June 17, 2026 at 2:54 am

    it seems like … would introduce a lot of overhead.

    You may assume it does, but do you actually know this? You should not make any assumptions if anything is faster or slower without actually running a profiler or benchmarking the code in some other way. If you look at the source, you can see that uniform_int_distribution‘s constructor merely assigns the min and max values – which should be really insignificant overhead. Especially if you take into account that the actual random number generation will be a much more complex operation than two assignments. So I’d suggest you leave the code as is, and if your program is too slow, you can always profile and then optimize.

    Edit: To quote Djikstra: “Premature optimization is the root of all evil”. Way too often, programmer’s write more complex code than need be, simply because they think it will be faster. Don’t do it – only start optimizing when there are speed problems.

    Anyway, to answer your questions: param() is a member of uniform_int_distribution. It takes an object of type uniform_int_distribution::param_type. You can use it like so:

    using namespace boost::random;
    
    // Create an uniform_int_distribution object
    uniform_int_distribution<> dist(1, 6);
    
    // Create a params object
    uniform_int_distribution::param_type newParams(10, 500);
    
    // The following will reconfigure dist to have 10 and 500 as 
    // min and max value
    dist.param(newParams);
    

    This way, you can reconfigure a single distribution object as often as you like. But the “overhead” will likely be the same as constructing a new distribution object.

    Another way you can ensure that the object is only created once:

    int roll_die()
    {
        static boost::random::uniform_int_distribution<> dist(1, 6);
        return dist(gen);
    }
    

    Declaring variables inside of functions as static has a similar effect as if the variable where global, but it’s only visible in the function’s scope.

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

Sidebar

Related Questions

I must generate a random number using boost libraries, I use this code: boost::mt19937
Can you tell me any ways to generate non-uniform random numbers? I am using
I want to generate some random double numbers using default_random_engine and uniform_real_distribution in header
Using this basic boost example on boost's website if I have code like this:
I'm trying to generate a random 64bit unsigned integer using boost random, but I'm
I am using this code to generate a random permutation of a vector using
I am using GCC 4.6.3 and was trying to generate random numbers with the
Right now I'm generating a random enumerator using boost's random library. Basically I'm using
I am trying to get real random values using boost::random libraries. This is my
I'm trying to generate a random real number between 0 and 1, using the

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.