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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:18:45+00:00 2026-06-13T07:18:45+00:00

I’m trying to create a sensible class hierarchy for a congruential random number generator

  • 0

I’m trying to create a sensible class hierarchy for a congruential random number generator class (which means that it has 3 integer parameters – M, a, b). I decided that the base class should provide an interface for derived ones (so it should be abstract), and M, a, b should be static const in derived classes (as they are the same throughout a derived class).

As generate() function is the same for all congruential random number generators, its definition should be placed inside the base class. The problem is that this function uses all of M, a, b, but these can’t be be made static const‘s in the base class.

To illustrate a problem, a provided one of the possible solutions. However, I’m not fully satisfied with it, as it creates 3 extra long long variables for each instance of a derived class, so I wonder if a more elegant design can be proposed.

class RandomGenerator{
   protected:
       unsigned int seed;

       const long long int M;
       const long long int a;
       const long long int b;

   public:
       RandomGenerator(unsigned int, long long, long long, long long);

       virtual long double generate()  const = 0;
};

long double RandomGenerator::generate() const{
   static long long prv = seed;

   return (long double) (prv = (a * prv + b) % M) / (M-1);
} 

class RandU : public RandomGenerator {
    private:
       static const long long M = 2147483648LL;
       static const long long a = 65539;
       static const long long b = 0;

   public:
       RandU(unsigned int);

       virtual long double generate() const;
 };


RandU::RandU(unsigned int nseed): RandomGenerator(nseed, M, a, b){}

long double RandU::generate() const{
    return RandomGenerator::generate();
 }
  • 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-13T07:18:45+00:00Added an answer on June 13, 2026 at 7:18 am

    One way to solve this is to add a class to the hierarchy:

    class RandomGenerator {
    protected:
      unsigned int seed;
    
    public:
      RandomGenerator(unsigned int, long long, long long, long long);
    
      virtual long long getM() = 0;
      virtual long long geta() = 0;
      virtual long long getb() = 0;
    
      virtual long double generate() const;
    };
    
    
    inline long double RandomGenerator::generate() const {
      static long long prv = seed;
      return (long double) (prv = (a * prv + b) % M) / (M-1);
    } 
    
    class GeneralRandomGenerator : public RandomGenerator
    {
      const long long int M;
      const long long int a;
      const long long int b;
    public:
      virtual long long getM() { return M; }
      virtual long long geta() { return a; }
      virtual long long getb() { return b; }
    };
    
    
    class RandU : public RandomGenerator {
    private:
      static const long long M = 2147483648LL;
      static const long long a = 65539;
      static const long long b = 0;
    
    public:
      RandU(unsigned int);
    
      virtual long long getM() { return RandU::M; }
      virtual long long geta() { return RandU::a; }
      virtual long long getb() { return RandU::b; }
    
      virtual long double generate() const;
    };
    
    
    RandU::RandU(unsigned int nseed): RandomGenerator(nseed, M, a, b){}
    
    inline long double RandU::generate() const {
      return RandomGenerator::generate();
    }
    

    As you can see, I’ve derived GeneralRandomGenerator from RandomGenerator. The former now contains members for M, a and b, while the latter provides pure virtual access functions getM(), geta() and getb(). In the GeneralRandomGenerator those access functions are implemented to return the members.

    But in RandU, which still derived from the highest level, the accessors are defined to return the values of the static members.

    This way, the generate() function of the highest level can access what it needs through the accessor functions, while the values really come from either static or non-static members, or potentially from somewhere completely different. The advantage clearly is that RandU won’t take any space for M, a and b because those members don’t exist in it.

    The disadvantage however is that even in GeneralRandomGenerator access to M etc. is performed by calling a virtual function, which mean poorer performance than what would be possible by coding access to the static members directly.

    One way to avoid even that is to do provide the generalised for of generate() as a separate function outside the classes and pass M, a and b as arguments:

    namespace general
    {
      inline long double generate(long long a, long long b, long long M) const {
        static long double prv = seed;
        return (prv = (a * prv + b) % M) / (M-1);
      }
    }
    
    class GeneralRandomGenerator
    {
    protected:
      unsigned int seed;
    
    public:
      long long a;
      long long b;
      long long M;
    
      virtual long double generate() const {
        return general::generate(a,b,M);
      }
    };
    
    class RandomU
    {
    private:
      static long long a;
      static long long b;
      static long long M;
    public:
      virtual long double generate() const {
        return general::generate(a,b,M);
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.