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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:26:53+00:00 2026-05-16T06:26:53+00:00

Setting: A pseudo-random pattern has to be generated. There are several ways / or

  • 0

Setting:
A pseudo-random pattern has to be generated. There are several ways / or algorithms availible to create different content. All algorithms will generate a list of chars (but could be anything else)… the important part is, that all of them return the same type of values, and need the same type of input arguments.

It has to be possible to call a method GetRandomPattern(), which will use a random one of the algorithms everytime it is called.

My first aproach was to put each algorithm in it’s own function and select a random one of them each time GetRandompattern() is called. But I didn’t come up with another way of choosing between them, than with a switch case statement which is unhandy, ugly and inflexible.

class PatternGenerator{
 public:
  list<char> GetRandomPattern();
 private:
  list<char>GeneratePatternA(foo bar);
  list<char>GeneratePatternB(foo bar);
  ........
  list<char>GeneratePatternX(foo bar);
}

What would be a good way to select a random GeneratePattern function every time the GetRandomPattern() method is called ?

Or should the whole class be designed differently ?

Thanks a lot

  • 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-16T06:26:54+00:00Added an answer on May 16, 2026 at 6:26 am

    Thank you for all your great input.
    I decided to go with function pointers, mainly because I didn’t know them before and they seem to be very powerfull and it was a good chance to get to know them, but also because it saves me lot of lines of code.

    If I’d be using Ruby / Java / C# I’d have decided for the suggested Strategy Design pattern 😉

    class PatternGenerator{
     typedef list<char>(PatternGenerator::*createPatternFunctionPtr);
    public:
     PatternGenerator(){
      Initialize();
       }
     GetRandomPattern(){
      int randomMethod = (rand()%functionPointerVector.size());
      createPatternFunctionPtr randomFunction = functionPointerVector.at( randomMethod );
      list<char> pattern = (this->*randomFunction)();
      return pattern;
      }
    private:
      void Initialize(){
       createPatternFunctionPtr methodA = &PatternGenerator::GeneratePatternA;
       createPatternFunctionPtr methodB = &PatternGenerator::GeneratePatternB;
       ...
       functionPointerVector.push_back( methodA );
       functionPointerVector.push_back( methodB );
       }
      list<char>GeneratePatternA(){
       ...}
      list<char>GeneratePatternB(){
       ...}
      vector< createPattern > functionPointerVector;
    

    The readability is not much worse as it would have been with the Design Pattern Solution, it’s easy to add new algorithms, the pointer arithmetics are capsuled within a class, it prevents memory leaks and it’s very fast and effective…

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

Sidebar

Related Questions

for my program I need pseudo random integers with different ranges. Until now I
Setting up the sandboxes for all these option is not feasible right now. So
Setting up environment variables for hundreds of tests get old very quick. Is there
There are a couple of sources for random in LoadRunner scenarios: rand () function
I wonder why there is a memcache.hash_strategy php.ini setting. The manual says: Controls which
I have a site where provide pseudo subdomain based sites. Each subdomain has a
Knows someone a solution for setting a CSS gradient on a pseudo element in
Setting up a project structure; I am thinking is it better to have somewhat
Setting default values for System Configuration fields is well understood (via Chapagain and blog
Setting up new git installations. On one Windows laptop, I'm running (under cygwin): git

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.