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

  • Home
  • SEARCH
  • 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 592807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:46:43+00:00 2026-05-13T15:46:43+00:00

I have the following code: Class B { void generator() { // creating random

  • 0

I have the following code:

Class B {

void generator()
{
    // creating random number generator
    boost::mt19937 randgen(static_cast<unsigned int>(std::time(0)));
    boost::normal_distribution<float> noise(0,1);
    boost::variate_generator<boost::mt19937, 
        boost::normal_distribution<float> > nD(randgen, noise);


    for (int i = 0; i < 100; i++)
    {
        value = nD();
        // graph each value
    }
}
};

Class A {

void someFunction()
{
    for(int i = 1; i <=3; i++)
    {
        std::shared_ptr<B> b;
        b.reset(new B());
        b->generator();
    }
}
};

I wish to execute the above code multiple times in rapid succession to produce multiple graphs. I have also reviewed this stackoverflow question which is similar but the caveat states that when time(0) is used and the member function is called in rapid succession then you will still likely get the same sequence of numbers.

How might I overcome this problem?

EDIT: I’ve tried making randgen static in Class B, also tried making it a global variable in Class A, but each time the 3 graphs are still the same. I’ve also tried seeding from the GetSystemTime milliseconds. I must be missing something.

  • 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-05-13T15:46:43+00:00Added an answer on May 13, 2026 at 3:46 pm

    One way would be to not reseed the random number generator every time you execute your code.

    Create the generator and seed it once, then just continue to use it.

    That’s assuming you’re calling that code multiple times within the same run. If you’re doing multiple runs (but still within the same second), you can use another differing property such as the process ID to change the seed.

    Alternatively, you can go platform-dependent, using either the Windows GetSystemTime() returning a SYSTEMTIME structure with one of its elements being milliseconds, or the Linux getTimeOfDay returning number of microseconds since the epoch.

    Windows:

    #include <windows.h>
    SYSTEMTIME st;
    GetSystemTime (&st);
    // Use st.wSecond * 100 + st.wMillisecs to seed (0 thru 59999).
    

    Linux:

    #include <sys/time.h>
    struct timeval tv;
    gettimeofday (&tv, NULL);
    // Use tv.tv_sec * 100 + (tv.tv_usec / 1000) to seed (0 thru 59999).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 308k
  • Answers 308k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What is GNU Fortran? g77 consists of several components: A… May 13, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer I think you're looking for server side includes: http://en.wikipedia.org/wiki/Server_Side_Includes. To… May 13, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer I don't really have an answer - just a few… May 13, 2026 at 9:43 pm

Related Questions

I am playing with class generation ( one class for a table - inheritance
I have the following mapping: <?xml version='1.0' encoding='utf-8'?> <hibernate-mapping assembly='Core' namespace='Core.Models' xmlns='urn:nhibernate-mapping-2.2'> <class name='Basket'>
Here's the scenario: Platform: VS2005 and language is VC++ Situation: There's just 1 assembly
I'm having a very big struct in an existing program. This struct includes a
Update: Replaced the destructor example with a straight up method call example. Hi, If

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.