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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:50:04+00:00 2026-05-18T09:50:04+00:00

I must generate a random number using boost libraries, I use this code: boost::mt19937

  • 0

I must generate a random number using boost libraries, I use this code:

boost::mt19937 gen;
boost::uniform_int<> dist(kUIMinManPort, kUIMaxManPort);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> >
var(gen, dist);
unsigned int value = (unsigned int)var();
return boost::lexical_cast<std::string>(value);

Obviously I import all necessary libraries. Well the code compiles but the problem is that I obtain the same numbers….

OK OK… do not worry, I am not such a newbie when talking about casual (or better pseudo-casual) number generation. I know that we must provide a seed and that, depending on this seed, a sequence of pseudocasual numbers will be provided.

So my code becomes this:

boost::mt19937 gen(static_cast<unsigned int>(std::time(0)));
boost::uniform_int<> dist(kUIMinManPort, kUIMaxManPort);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> >
var(gen, dist);
unsigned int value = (unsigned int)var();
return boost::lexical_cast<std::string>(value);

Well, the problem is that we get almost the same number everytime I call this function (inside a for cycle).
I suspect that the time depending seed provided to the generation core of the boost random library does not vary during the period of time of a for cycle, that’s why get almost the same number everytime I run a cycle and get a random number… The question is: how to solve this problem in an efficient way??? I suppose a best practice is given… Well, I’m not the only one having such a problem 🙂

Thanks…

  • 1 1 Answer
  • 3 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-18T09:50:05+00:00Added an answer on May 18, 2026 at 9:50 am
    static boost::mt19937 gen(static_cast<unsigned int>(std::time(0)));
    

    The static makes sure that the generator is only created once. The problem is that time isn’t changing fast enough. If you call your function in the same millisecond, you’ll get the exact same results. Unfortunately, your code is just that fast that you’re calling it in the same millisecond.

    Making the generator static (or a singleton pattern, or a global variable…) will solve the issue.

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

Sidebar

Related Questions

This is the code I am using for the encryption but it generate an
This question is NOT about how to use any language to generate a random
Must be a way looping through this code: private void loadSprites() { this.sprites[0] =
Generate a random number in range [x..y] where x and y are any arbitrary
I need to generate random strings in vb.net, which must consist of (randomly chosen)
I'm trying to generate an XML document from C# List. The xml document must
Must Support IE6 and must Validate vs XHTML Strict 1.0! This is tricky to
There must be a simple solution to this problem but for the love of
This must be a very stupid question because nobody else is asking it. I'm
This must be a duplicate. But with so many NSPredicate questions out there, I

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.