-1

I'm trying to create a class that can generate a random variable. I want to be able to generate any random type based on the templated value at runtime. I figured I can do this by making this function works for ints, strings, and double data types by coding each one specifically.

template <typename T>
class Generator{
    private:
        int upper_bound;
        int lower_bound;
        int size;
        vector<T> data;
        vector<int> keys;
    public:  
        Generator(int size, int lower_bound, int upper_bound){
            this->size = size;
            this->upper_bound = upper_bound;
            this->lower_bound = lower_bound;
        }
    
        void generate_keys(){
            //key generater
            int rand_num;
            int row = 0;
            
            //uses srand and rand to get generator random keys and random data
            srand(time(0));           
            cout << "Generating the keys now..." << endl;
            cout << "upper bound - " << get_upper_bound() << endl;
            for(int i=0;i < size;i++){
                //rand_num = dist(rng);
                rand_num = (rand()%upper_bound)+lower_bound;
                cout << rand_num << " ";               
                keys.push_back(rand_num);  
                
                row++;
                if(row == 3){
                    cout << endl;
                    row = 0;
                }
            }
            cout << endl;    
        }
    
        //templated so this function can work with strings, doubles, and ints
        void generate_data(){
            
        }
bob games
  • 1
  • 1

1 Answers1

-2

You can use template specialization for generating random numbers in different data types.

use this code logic for floating-point random number generation

printf( "%f\t", (float)rand() / (float)(RAND_MAX / (max * 2)) - max);
  • https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad – bolov Mar 22 '22 at 14:00
  • What has your printf to do with the question? How is your link to template specialization related to "templated value at runtime" from OP? No idea what your answer will help... – Klaus Mar 22 '22 at 14:04