A Better Random Number -C++ The random number generator provided in C++ is reall
ID: 3732170 • Letter: A
Question
A Better Random Number -C++
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.
Question
How many files are we supposed to have? Such as, do I have to include random.cpp, random.h, and main.cpp?
Explanation / Answer
here is your class : ---------->>>>>>>>>>
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
class Integer{
int d;
public:
Integer(){
d = 0;
}
Integer(int d){
this->d= d;
}
int get()const{
return d;
}
void set(int d){
this->d = d;
}
};
class Double{
double d;
public:
Double(){
d = 0;
}
Double(double d){
this->d = d;
}
double get()const{
return d;
}
void set(double d){
this->d = d;
}
friend ostream& operator<<(ostream& out,const Double &oth){
out<<oth.d;
return out;
}
};
class Random{
vector<double> vect;
int suff;
int cur;
void shuffle(){
int ind1;
int ind2;
double temp;
for(int i = 0;i<3000;i++){
ind1 = rand()%250;
ind2 = rand()%250;
temp = vect[ind1];
vect[ind1] = vect[ind2];
vect[ind2] = temp;
}
suff = 230;
cur = 0;
}
void fillVect(double max,double min){
for(int i = 0;i<250;i++){
vect[i] = (((double) rand() / (double) RAND_MAX) * (max - min)) + min ;
}
shuffle();
}
public:
Random(){
vect.clear();
vect.reserve(250);
fillVect(RAND_MAX,0);
}
Random(double min,double max){
vect.clear();
vect.reserve(250);
fillVect(max,min);
}
Random(Double min,Double max){
vect.clear();
vect.reserve(250);
fillVect(max.get(),min.get());
}
Random(unsigned int seed){
srand(seed);
vect.clear();
vect.reserve(250);
}
int nextInt(){
if(cur >= suff){
shuffle();
}
return (int)(vect[cur++]);
}
Integer nextInteger(){
if(cur >= suff){
shuffle();
}
return Integer((int)vect[cur++]);
}
double nextDbl(){
if(cur >= suff){
shuffle();
}
return vect[cur++];
}
Double nextDouble(){
if(cur >= suff){
shuffle();
}
return Double(vect[cur++]);
}
void setRange(double min,double max){
fillVect(max,min);
}
void setRange(Double min,Double max){
fillVect(max.get(),min.get());
}
};
int main(){
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.