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

The Archive Base Latest Questions

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

I am working on a C++ class that uses a rand() in the constructor.

  • 0

I am working on a C++ class that uses a rand() in the constructor. I would really like for this class to take care of itself in pretty much every way, but I’m not sure where to seed rand().

If I seed rand() in the constructor, it will be seeded every time a new instance of my object type is constructed. So if I were to create 3 objects in sequence, they would all be created in the same second and therefore have the same seed for rand(), producing the exact same data for each of the 3 instances of the object.

I would like to seed rand() within the class code, rather than having to do it in the main function of my program before I create the object. I thought of doing a static bool seeded; variable that signifies whether or not rand() has been seeded yet, but I’m not really sure how to initialize it to false at the creation of the class.

My idea goes something like

myConstructor(){
    if(!seeded){
        srand(time(NULL));
        seeded = true;
    }

    myVariable = rand()%maxVal;
}

I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program. It is my understanding that changing this static value to true would carry across all instances of the object if it were static, and would therefore only execute the seed function the very first time that object type is created.

  • 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-13T21:26:46+00:00Added an answer on June 13, 2026 at 9:26 pm

    I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program.

    // my_class.h
    class my_class {
    public:
      // ...
    private:
      static bool seeded;
    };
    
    // my_class.cpp
    bool my_class::seeded = false;
    

    Make sure to define seeded in the implementation file. Otherwise every file which includes your header will get its own definition of the static member and it could also cause linker issues as it can be defined more than once.

    On a side note, if the static member were of a const integral type, you could assign it at the point of declaration.

    Another option would be this, and personally I would prefer it for this task:

    my_class::my_class()         
    {
        static bool seeded = false;
        if(!seeded) {
            srand(time(NULL));
            seeded = true;
        }
    
        myVariable = rand() % maxVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on someone's code and they have a constructor that uses: class qwerty
Suppose I'm working on a MongoMapper class that looks like this: class Animal include
I'm working on an user interaction class that uses a set of driver classes
I'm working on a c++ code that uses SDL/opengl. Is this possible to create
I'm working on a C# project that uses System.Data.OleDb.OleDbCommand class to create and alter
I'm working with a very simple web service that uses a base class to
So I'm working on a class project that uses MFC. I never had to
I'm working on a game (C#) that uses a Robocode-like programming model: participants inherit
I am working on improving a stream reader class that uses a BinaryReader .
I am currently working on a hand of cards class that uses enums that

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.