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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:33:56+00:00 2026-06-09T16:33:56+00:00

I’m trying to generate random numbers with boost, and I’m having trouble getting it

  • 0

I’m trying to generate random numbers with boost, and I’m having trouble getting it to work in a way that keeps everything encapsulated. I’ve got this piece of code in main(), which allows me to use roll() to generate a random number from [0,1)

boost::mt19937 rng(seed);
boost::uniform_01<> uniform_p;
boost::variate_generator< boost::mt19937, boost::uniform_01<> >
    roll(rng, uniform_p);

Now I want to stick this in its own header/cpp file, with one function to obtain a seed and create roll, and others that call roll. But nothing can seem to see roll, and I’m pretty sure it will go out of scope after the function that calls it goes out of scope. I’ve tried playing around with static and extern, I’ve tried making roll part of a class so it won’t go out of scope, but nothing seems to work.

  • 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-09T16:33:58+00:00Added an answer on June 9, 2026 at 4:33 pm

    The following is a Matlab-like interface which I like to use for the generation of random numbers. You have to call seed() before using rand(). As Greg pointed out, scoped_ptr is handy to store the static variables (the Mersenne twister and the variate generator) which are needed. For thread-safety, protect these ressources with mutexes.

    random.hpp

    #ifndef RANDOM_HPP
    #define RANDOM_HPP
    
    void seed(unsigned s);
    double rand();
    
    #endif // RANDOM_HPP
    

    random.cpp

    #include "random.hpp"
    #include <boost/random/mersenne_twister.hpp>
    #include <boost/random/uniform_01.hpp>
    #include <boost/random/variate_generator.hpp>
    #include <boost/scoped_ptr.hpp>
    
    static boost::scoped_ptr<boost::mt19937> twister;
    static boost::scoped_ptr<boost::variate_generator<boost::mt19937&,
                                                      boost::uniform_01<> > > vargen;
    
    void seed(unsigned s)
    {
        twister.reset(new boost::mt19937(s));
        vargen.reset(new boost::variate_generator<boost::mt19937&,
                                                  boost::uniform_01<> >(
                         *twister, boost::uniform_01<>()));
    }
    
    double rand()
    {
        assert(vargen.get() != 0);
        return (*vargen)();
    }
    

    main.cpp

    #include "random.hpp"
    #include <iostream>
    
    int main()
    {
        seed(42);
        for (int i = 0; i < 10; ++i)
            std::cout << rand() << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.