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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:29:04+00:00 2026-05-27T11:29:04+00:00

I’m reading some book, and I’m given some timer implementation. However there is no

  • 0

I’m reading some book, and I’m given some timer implementation. However there is no real explanation about the timer in the book, and I’m wondering why he is doing certain things.

Code:

//  Desc:   Use this class to regulate code flow (for an update function say)
//          Instantiate the class with the frequency you would like your code
//          section to flow (like 10 times per second) and then only allow 
//          the program flow to continue if Ready() returns true

class Regulator
{
private:

  //the time period between updates 
  double m_dUpdatePeriod;

  //the next time the regulator allows code flow
  DWORD m_dwNextUpdateTime;


public:


  Regulator(double NumUpdatesPerSecondRqd)
  {
    m_dwNextUpdateTime = (DWORD)(timeGetTime()+RandFloat()*1000);

    if (NumUpdatesPerSecondRqd > 0)
    {
      m_dUpdatePeriod = 1000.0 / NumUpdatesPerSecondRqd; 
    }

    else if (isEqual(0.0, NumUpdatesPerSecondRqd))
    {
      m_dUpdatePeriod = 0.0;
    }

    else if (NumUpdatesPerSecondRqd < 0)
    {
      m_dUpdatePeriod = -1;
    }
  }


  //returns true if the current time exceeds m_dwNextUpdateTime
  bool isReady()
  {
    //if a regulator is instantiated with a zero freq then it goes into
    //stealth mode (doesn't regulate)
    if (isEqual(0.0, m_dUpdatePeriod)) return true;

    //if the regulator is instantiated with a negative freq then it will
    //never allow the code to flow
    if (m_dUpdatePeriod < 0) return false;

    DWORD CurrentTime = timeGetTime();

    //the number of milliseconds the update period can vary per required
    //update-step. This is here to make sure any multiple clients of this class
    //have their updates spread evenly
    static const double UpdatePeriodVariator = 10.0;

    if (CurrentTime >= m_dwNextUpdateTime)
    {
      m_dwNextUpdateTime = (DWORD)(CurrentTime + m_dUpdatePeriod + RandInRange(-UpdatePeriodVariator, UpdatePeriodVariator));

      return true;
    }

    return false;
  }
};

isEqual() checks whether the difference between two given doubles is smaller than 0.00…1

RandInRange() returns a random number within the given range

I don’t really understand the part why he is adding a random number with a given range to the m_dwNextUpdateTime. Even though he ‘explains’, it doesn’t seem to make any sense to me.

Can anyone help?

  • 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-27T11:29:05+00:00Added an answer on May 27, 2026 at 11:29 am
    // This is here to make sure any multiple clients of this class
    //have their updates spread evenly
    

    The randomness is introduced to lower contention when you run the same code many times concurrently. If all clients use the exact same interval, they might all “wake up” at the exact same time, which can be unfortunate load-wise. Introducing some random variations makes them spread out their activity more.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is

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.