2

I am trying to use the C++ random library by initializing it in the ctor and using it in private member functions. I am able to make the simple code in the highest rated answer from Generating random integer from a range work in a main with no functions, but I cannot get it to compile in a basic class with a header. What is needed to make the random library work in this kind of structure?

Randy.h

#ifndef Randy_H_
#define Randy_H_

#include <random>

class Randy
{
    public:
        Randy();
        unsigned short int Doit();
    private:
        std::random_device rd;
        std::mt19937 rng();
        std::uniform_int_distribution<unsigned short int> InRange1();
        std::uniform_int_distribution<unsigned short int> InRange2();

        unsigned short int GenerateBar();
};
#endif

Randy.cpp

#include <random>
#include "Randy.h"

using namespace std;

Randy::Randy()
{
    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<unsigned short int> InRange1(0, 180);
    std::uniform_int_distribution<unsigned short int> InRange2(150, 15000);
}

unsigned short int Randy::Doit()
{
    unsigned short int x = GenerateBar();
    return x;
}

unsigned short int Randy::GenerateBar()
{
    unsigned short int oof = InRange1(rng);
    unsigned short int rab = InRange2(rng);
    unsigned short int bar = oof + rab;
    return bar;
}

g++ on Windows 10 complains as follows:

Randy.cpp: In member function 'short unsigned int Randy::GenerateBar()':
Randy.cpp:22:42: error: no matching function for call to 'Randy::InRange1(<unresolved overloaded function type>)'
     unsigned short int oof = InRange1(rng);
                                          ^
In file included from Randy.cpp:2:
Randy.h:14:59: note: candidate: 'std::uniform_int_distribution<short unsigned int> Randy::InRange1()'
         std::uniform_int_distribution<unsigned short int> InRange1();
                                                           ^~~~~~~~
Randy.h:14:59: note:   candidate expects 0 arguments, 1 provided
Randy.cpp:23:42: error: no matching function for call to 'Randy::InRange2(<unresolved overloaded function type>)'
     unsigned short int rab = InRange2(rng);
                                          ^
In file included from Randy.cpp:2:
Randy.h:15:59: note: candidate: 'std::uniform_int_distribution<short unsigned int> Randy::InRange2()'
         std::uniform_int_distribution<unsigned short int> InRange2();
                                                           ^~~~~~~~
Randy.h:15:59: note:   candidate expects 0 arguments, 1 provided

Thanks in advance...

After reviewing the suggested hint Why should I prefer to use member initialization lists?, I have worked out a structure to solve the problem. I offer it here as it could save others many hours of puzzling out how to navigate the invisible terrain of C++.

Randy2.h

#ifndef RANDY_H
#define RANDY_H

#include <random>

class Randy
{
    public:
        Randy();
        Randy(int seed);        
        unsigned short int Doit();
    private:
        int mySeed { 17 };
        std::mt19937 genRand;  
        std::uniform_int_distribution<unsigned short int> InRange1;
        std::uniform_int_distribution<unsigned short int> InRange2;

        unsigned short int GenerateBar();
};
#endif

Randy2.cpp

#include <random>
#include "Randy2.h"

using namespace std;

Randy::Randy()
    :
        genRand(mySeed),
        InRange1(0, 180),
        InRange2(150, 15000)
{
    // No ctor body needed
}            

Randy::Randy(int seed)
    :
        genRand(seed),
        InRange1(0, 180),
        InRange2(150, 15000)
{
    // No ctor body needed
}   

unsigned short int Randy::Doit() 
{
    unsigned short int x = GenerateBar();
    return x;
}

unsigned short int Randy::GenerateBar()
{
    unsigned short int oof = InRange1(genRand);
    unsigned short int rab = InRange2(genRand);
    unsigned short int bar = oof - rab;
    return bar;
}
  • This doesn’t address the question, but the convention is that preprocessor macros are written in all caps. So `Randy_H_` would usually be `RANDY_H_`. – Pete Becker Apr 23 '21 at 22:19

0 Answers0