The random number generator provided in C++ is really a pseudo-random number gen
ID: 3728701 • Letter: T
Question
The random number generator provided in C++ is really a pseudo-random number generator. What this means is that if you examine the output you will find that the numbers repeat in a pattern. For this assignment I want you to create a better random number generator.
In object oriented programming what we really want to do is reuse code. Since the rand function does a great deal of what we desire we can use it and simply add functionality to make it more efficient. The class you are going to be creating in this assignment is called Random and it will be loosely based on the Java Random class.
Here is the functionality
Constructor Summary
Random() - Default constructor
Random(double min, double max); - generates random numbers between min and max.
Random(Double min, Double max); - generates random numbers between min and max using class Double
Random(int seed); - seed the random number generator
Function Summary
int nextInt() - Returns the next random number as an int
Integer nextInteger() - Returns the next random number as an Integer class
double nextDbl() - Returns the next random number as a primitive double
Double nextDouble() - Returns the next random number as a Double class
void setRange(double min, double max) - Sets the rage and recreates random numbers
void setRange(Double min, Double max) - Sets the range and recreates the random numbers.
Specifics
Basically, this is a wrapper around the function rand. You will put a vector in your data section to hold 250 primitive double values. When one of the constructors is called you will clear the vector and fill it with new random doubles. For this you should have a private function called fillVect which will generate random doubles in whatever range is specified.. Once the vector is filled you will want to shuffle the values in the vector around. I will leave it up to you to determine how to shuffle the vector but it may be easy to simply swap values at two random indexes a bunch of times. This function should be called shuffle and should be defined as private.
To generate random doubles in a range you can use the following algorithm:
double r = (((double) rand() / (double) RAND_MAX) * (max - min)) + min ;
Where min and max are double values passed into the private function called fillVect. RAND_MAX is a constant that is added when you include iostream. You do not need to do anything to use it.
Constructors
The default constructor and the constructor that takes the seed should simply fill the vector with numbers in the range of 0 to RAND_MAX.
All constructors except the constructor that takes the seed will use the time function to seed the random number generator.
The constructor that takes seed should pass the value to srand for seeding.
Functions
The next functions should return the next value in the vector as whatever type specified.
Please note that when you have gone through 90% or more of the vector you should reshuffle and start from the beginning
The setRange functions should clear the vector and generate new random numbers for the vector. The vector should also be shuffled once it has been filled. Note that this class uses only one vector of random values.
Finally, try to avoid using literal values. You know that you should use 250 numbers and you need to reshuffle at 90%. It would be best to use constants define these values.
Explanation / Answer
Hello, I have a solution for you. Implemented everything as needed. Defined following things in this answer.
//Random.cpp
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<vector>
#define MAX_CAPACITY 250
#define THRESHOLD_PERCENTAGE .90 //90%
using namespace std;
class Random{
double minimum;//to store minimum limit
double maximum; //to store maximum limit
vector<double> values; // to store random values
int index; // to keep the track of elements in vector
//method to fill the vector with random values in range
void fillVector(double min,double max){
index=0;//resetting index to 0
values.clear();
//generating 250 random values, adding to the vector
for(int i=0;i<MAX_CAPACITY;i++){
double r = (((double) rand() / (double) RAND_MAX) * (max - min)) + min ;
values.push_back(r);
}
//shuffling the values
shuffle();
}
//method to shuffle the vector
void shuffle(){
for(int i=0;i<MAX_CAPACITY/2;i++){
//generating two random indices
int randIndex1=rand()%values.size();
int randIndex2=rand()%values.size();
//swapping the values
double temp=values[randIndex1];
values[randIndex1]=values[randIndex2];
values[randIndex2]=temp;
}
}
//method to check if the values in vector are 90% iterated,
//if yes, refilling and reshuffling vector
void refillVectorIfNeeded(){
if(index>=(MAX_CAPACITY*THRESHOLD_PERCENTAGE)){
fillVector(minimum,maximum);
}
}
public:
//default constructor
Random(){
srand(time(NULL)); //seeding with time(NULL)
fillVector(0,RAND_MAX); //filling vector
minimum=0;//setting minimum and maximum values accordingly
maximum=RAND_MAX;
}
//constructor with minimum and maximum range specification
Random(double min, double max){
srand(time(NULL));//seeding with time(NULL)
minimum=min;//setting minimum and maximum values accordingly
maximum=max;
fillVector(minimum,maximum);//filling vector
}
//constructor with seed value specification
Random(int seed){
srand(seed);//seeding with given value
fillVector(0,RAND_MAX);//filling vector
minimum=0;
maximum=RAND_MAX;
}
//returns the next integer
int nextInt(){
int number=values[index];
index++;//incrementing the index
refillVectorIfNeeded();//refilling the vector if needed
return number;
}
//returns the next double
double nextDbl(){
double number=values[index];
index++;
refillVectorIfNeeded();
return number;
}
//sets the range
void setRange(double min, double max){
minimum=min;
maximum=max;
fillVector(minimum,maximum);
}
};
int main(){
//using default constructor
Random r1;
cout<<"Random object using default constructor"<<endl;
cout<<"using nextInt(): "<<r1.nextInt()<<endl;
cout<<"using nextDbl(): "<<r1.nextDbl()<<endl;
cout<<"Random object specifying minimum and maximum values (10.5,98.7)"<<endl;
//using min and max range
Random r2(10.5,98.7);
cout<<"using nextInt(): "<<r2.nextInt()<<endl;
cout<<"using nextDbl(): "<<r2.nextDbl()<<endl;
cout<<"Random object specifying seed value of 12"<<endl;
//using custom seed value
Random r3(12);
cout<<"using nextInt(): "<<r3.nextInt()<<endl;
cout<<"using nextDbl(): "<<r3.nextDbl()<<endl;
return 0;
}
/*OUTPUT*/
Random object using default constructor
using nextInt(): 11135
using nextDbl(): 24215
Random object specifying minimum and maximum values (10.5,98.7)
using nextInt(): 40
using nextDbl(): 75.6803
Random object specifying seed value of 12
using nextInt(): 77
using nextDbl(): 6639