Thread: putting a random numbers generator in a class

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    15

    putting a random numbers generator in a class

    I'm learning c++ so it's a newbie question.

    I what to put a random generator in a class because it is only used by the class.
    To declare and to initialize a random numbers generator I use this piece of code:

    Code:
    std::random_device seed;
    std::default_random_engine rand_gen(seed());
    but putting that code in a class it gives an error, for example

    Code:
    class TBoard {
    private:
    std::random_device seed;
    std::default_random_engine rand_gen(seed());
    };
    it's wrong. The solution I think is to put rand_gen(seed()) in the class constructor but I can't find the right solution. Could you help me? Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remove the "(seed())" from the member variable declaration and try using the constructor initialiser list.

    I believe the issue you're facing is that initialising in the member variable declaration requires a constexpr (or something like that), which seed() certainly isn't.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. not getting random numbers using Boost in a class
    By Spirro in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2014, 05:54 PM
  2. Random numbers generator & Division algorithm
    By newclearner in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 12:48 PM
  3. Random Number Generator Class
    By abachler in forum Windows Programming
    Replies: 4
    Last Post: 09-03-2008, 08:30 AM
  4. Replies: 6
    Last Post: 07-19-2006, 05:00 AM
  5. Random numbers generator
    By murand in forum C Programming
    Replies: 8
    Last Post: 03-18-2003, 05:38 AM

Tags for this Thread