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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:17:51+00:00 2026-05-16T16:17:51+00:00

for my program I need pseudo random integers with different ranges. Until now I

  • 0

for my program I need pseudo random integers with different ranges.
Until now I used the rand() function but it has it’s limitations.

I found the boost::random library to be a much better replacement but I didn’t want to create random generators all over the place.
( I need random integers in many classes, because it’s a stress test software that makes every decision pseudo-randomly ( -> a test run has to be repeatable by setting the same start seed ) ).

That’s why I capsuled boost::random away in my own class.

The idea behind this is to ease the use so that it is almost as straightforward as
the C++ rand() method

#include "boost/shared_ptr.hpp"
#include "boost/random.hpp"

class Random{
public:
   typedef boost::shared_ptr< Random > randomPtr;
   typedef boost::mt19937 randomGeneratorType;

   static randomPtr Get(){
      static randomPtr randomGen( new RandomGenerator() );
      return randomGen;
   }

   void SetSeed(int seed){
      randomGenerator.seed( seed );
   }

   int Random( int lowerLimit, int upperLimit ){
   boost::uniform_int<> distribution( lowerLimit, upperLimit );
   boost::variate_generator< randomGeneratorType&, boost::uniform_int<> >
   LimitedInt( randomGenerator , distribution );
   return LimitedInt();
   }

private:
   // prevent creation of more than one object of the LogManager class
   // use the Get() method to get a shared_ptr to the object
  Random():
    randomGenerator() //initialize randomGenerator with default constructor
  {}

  RandomGenerator( const RandomGenerator& orig ){};

  randomGeneratorType randomGenerator;
};

Generating a random number within a given range will now be as easy as

#include "Random.h"
  Random::Get()->SetSeed( 123123 );  // If you want to make the run repeatable
  int dice = Random::Get()->Random(1,6);

Question:
Is there anything wrong with this way of generating random numbers?
Large overhead I didn’t recognize ?
Pure Evil or outdated programming technique ?

( I’m still new to c++ and want to improve my skills, and I have found that Stack overflow is
the best place to get high quality advice )

  • 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-16T16:17:52+00:00Added an answer on May 16, 2026 at 4:17 pm

    Joe Gauterin demonstrated the issue, however it didn’t offered any solution 🙂

    The problem with shared state is the absence of reentrance: ie, executing twice the same method does not provide the same result. This is particularly critical in multithreaded situations because the global state may not always change at the same point in the program, thus leading to inconsistent results from one run to another.

    The solution is that each simulation should have its own “state” and then you would avoid the shared state.

    This can be accomplished in a number of ways: you could still use a “global” state but make it local to a thread, for example, thus the threads would not step on each others toes.

    The cleaner version, however, consists in storing this state somewhere, and the easier way is to have some kind of Context class, instantiated once per simulation, and which is an aggregate of the state of the simulation (for simulation-wide state).

    With that in mind:

    class Context
    {
    public:
      typedef boost::mt19937 RandomGeneratorType;
    
      void SetSeed(int seed){
         rg.seed( seed );
      }
    
      int Next( int lowerLimit, int upperLimit ) {
        boost::uniform_int<> distribution( lowerLimit, upperLimit );
        boost::variate_generator< randomGeneratorType&, boost::uniform_int<> >
        LimitedInt( rg, distribution );
        return LimitedInt();
      }
    
    private:
      RandomGeneratorType rg;
    };
    

    Then, pass the Context instance around in your simulation, and you can run as many as you wish in parallel.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why use a stamp at all? I use the following… May 16, 2026 at 10:25 pm
  • Editorial Team
    Editorial Team added an answer I'm pretty sure the default location providers for fine location… May 16, 2026 at 10:25 pm
  • Editorial Team
    Editorial Team added an answer You want to install SP1 for Visual Studio. See http://devnet.jetbrains.net/docs/DOC-1204 May 16, 2026 at 10:25 pm

Trending Tags

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

Top Members

Related Questions

I need a pseudorandom number generator algorithm for a assembler program assigned in a
I have a program in which I need to run multiple insert statements (dynamically
My Delphi 6 program need to place an image on each row of my
In my program, I need to run a external command in a Ubuntu environment
I need to write a program to analyze the performance of computer systems and
I need to create a .bat file to run java program. I have a
I have a program where I basically need to load Rich Text from a
I need to get a range (or whole range) of current buffer, run program
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
I'm trying to write a program which will pseudorandomly autogenerate (based on a seed

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.