Developing a Random class for generating random numbers
Before using this theme, it is recommended that you familiarize yourself with the theme:
Contents
- Task
- Solution
- 1. Declare internal variables min, max
- 2. Class constructors
- 3. Functions that generate single random numbers
- 4. Functions GetArrayInt() and GetArrayFloat(). Formation of sequences (arrays) of numbers
- 5. Functions SetMinMax(), GetMin(), GetMax(). Access to the internal variables
- 6. Function main(). Demonstration of using the Random class
- 7. The text of the entire program (abridged version)
- Related topics
Search other websites:
Task
Develop a Random class that contains facilities for generating sequences of random numbers. Demonstrate the class. Develop the program as a Console Application.
⇑
Solution
1. Declare internal variables min, max
In the first step, internal variables are introduced. In our case, in the private section, you need to enter variables with the following names:
- min – the lower limit of the range of random numbers;
- max – the upper limit of the range of random numbers.
Internal variables min, max are of type double. The class supports working with both integers and real numbers.
At this stage, the source code of the program is as follows:
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; // Class Random class Random { private: double min, max; // range generating }
⇑
2. Class constructors
In the constructors, the code for setting the initial value (starting point) is placed for further generating sequences
srand(time(NULL));
This means that in the constructor for each created instance of the class, its own starting point is created. Each instance of the class will generate its own unique sequence of random numbers.
The text of the class constructors is as follows
... // Class Random class Random { private: double min, max; // range generating public: // Class constructors // Constructor without parameters, initialize the number generator. // The default range is [0, 100]. Random(double min, double max) { srand(time(NULL)); // Correction if limits are incorrect if (max > min) { this->min = max; this->max = min; } else { this->min = min; this->max = max; } } // Constructor with two parameters that set the range // for generating random numbers Random(double min, double max) { srand(time(NULL)); this->min = min; this->max = max; } }
⇑
3. Functions that generate single random numbers
When generating random numbers, the basic methods of the class are methods for generating single random numbers. The class implements the generation of both integers and floating point numbers.
3.1. Functions NextInt(). Generating integers
To generate integers, two functions are used, which differ in input parameters. One of the functions (with two parameters) receives the minimum and maximum values as input parameters, which form the range of the received number. Another function (without parameters) uses the value of the internal class variables min, max to form a range of generated numbers.
// A function to generate a random integer within the specified bounds. // The range of numbers: [min, max] int NextInt(int min, int max) { // Get a random number - formula int num = min + rand() % (max - min + 1); return num; } // A function for generating a random integer // based on the data of the current instance. int NextInt() { return NextInt((int)this->min, (int)this->max); }
⇑
3.2. Functions NextFloat(). Generating floating point numbers
To work with floating point numbers, the class implements two overloaded functions NextFloat(). One of the functions returns a floating point number for the range of values (min, max), which is specified as a function parameter. The second function returns a floating point number for the range of values (min, max), which is specified in the internal fields of the class instance.
Both functions are designed in such a way that they allow you to get a floating point number with a given precision. The precision is specified as an integer, which is equal to the number of decimal places.
The function text is as follows:
// A function that generates a random floating point number with a specified precision. // Function gets 3 parameters: // - min - lower limit; // - max - upper limit; // - precision - precision, number of decimal places. double NextFloat(double min, double max, int precision = 2) { double value; // get a random number as an integer with order precision value = rand() % (int)pow(10, precision); // get a real number value = min + (value / pow(10, precision )) * (max - min); return value; } // An overloaded version of NextFloat(), the function receives a parameter - precision. // The range is determined based on the specified instance. double NextFloat(int precision = 2) { double value; // get a random number as an integer with order precision value = rand() % (int)pow(10, precision); // get a real number value = min + (value / pow(10, precision)) * (max - min); return value; }
⇑
4. Functions GetArrayInt() and GetArrayFloat(). Formation of sequences (arrays) of numbers
To extend the capabilities of the Random class, two additional functions have been introduced:
- the GetArrayInt() function returns a pointer to an array of randomly generated integers;
- the GetArrayFloat() function returns a pointer to an array of random floating point numbers.
The functions allocate memory for the resulting array. This memory needs to be freed after being used in the calling code.
// A method that returns an array of integers in the range // set by the min, max values of the current instance. // The input parameter count is the number of numbers in the array int* GetArrayInt(int count) { int* A = new int[count]; for (int i = 0; i < count; i++) A[i] = NextInt(); return A; } // A method that returns an array of real numbers in the range // that is set in the current instance. // Input parameters: // - count - the number of numbers in the array; // - precision - number of decimal places double* GetArrayFloat(int count, int precision = 2) { double* A = new double[count]; for (int i = 0; i < count; i++) A[i] = NextFloat(precision); return A; }
⇑
5. Functions SetMinMax(), GetMin(), GetMax(). Access to the internal variables
Sometimes it becomes necessary to change or read the range of values that are generated by the methods of an instance of the Random class. For this, the class implements access methods for the min, max variables.
// ----------- Methods to access the internal variables---------------- void SetMinMax(double min, double max) { this->min = min; this->max = min; } double GetMin() { return min; } double GetMax() { return max; }
⇑
6. Function main(). Demonstration of using the Random class
The main() function demonstrates the use of the Random class. The function text is as follows:
void main() { // 1. Declare class instances Random R1(0, 5); // constructor with 2 parameters is called Random R2; // constructor without parameters is called // 2. Checking the NextInt() method int d1 = R1.NextInt(); int d2 = R1.NextInt(); cout << "d1 = " << d1 << endl; cout << "d2 = " << d2 << endl; // 3. Demonstration of the GetArrayInt() method int* D = R1.GetArrayInt(20); // memory is allocated for array D cout << "Array D: "; for (int i = 0; i < 20; i++) cout << D[i] << " "; cout << endl; delete[] D; // do not forget to free memory // 4. Initialization with another instance of the class Random R3 = R1; double* X = R3.GetArrayFloat(10, 4); // the memory is allocated cout << "Array X: "; for (int i = 0; i < 10; i++) cout << X[i] << " "; cout << endl; delete[] X; // free memory // 5. Set new limits on instance R1 R1.SetMinMax(19, 13); // numbers in the range [13; 19] // Generate a new sequence in R1 double* X2 = R1.GetArrayFloat(10, 3); cout << "Array X2:"; for (int i = 0; i < 10; i++) cout << X2[i] << " "; cout << endl; delete[] X2; }
⇑
7. The text of the entire program (abridged version)
The abbreviated text of the Random class is as follows:
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; // Class Random class Random { private: double min, max; // range generating public: // ----------- Class constructors ---------------------- // Constructor without parameters, initialize the number generator. // The default range is [0, 100]. Random() { ... } // Constructor with two parameters that set the range // for generating random numbers Random(double min, double max) { ... } // ------ Methods for generating single random numbers -------- // A function for generating a random integer // based on the data of the current instance. int NextInt() { ... } // A function to generate a random integer within the specified limits. // Range of numbers: [min, max] int NextInt(int min, int max) { ... } // A function that generates a random floating point number with the specified precision. double NextFloat(double min, double max, int precision = 2) { ... } // An overloaded version of NextFloat () double NextFloat(int precision = 2) { ... } // ---------- Methods for generating sequences ------------------- // A method that returns an array of integers in the range // set by the min, max values of the current instance. int* GetArrayInt(int count) { ... } // A method that returns an array of real numbers in the range that is set on the specified instance. double* GetArrayFloat(int count, int precision = 2) { ... } // ----------- Methods to access the internal variables ---------------- void SetMinMax(double min, double max) { ... } double GetMin() { ... } double GetMax() { ... } }; void main() { ... }
⇑
Related topics
⇑