Question
The random number generator provided in C++ is reallypseudo-random number generator. What this means is that if youexamine the output you will find that the
The random number generator provided in C++ is reallypseudo-random number generator. What this means is that if youexamine the output you will find that the numbers repeat in apattern. For this assignment I want you to create a better randomnumber generator.
In object oriented programming what really want to do is reusecode. Since the rand function does a great deal of what we desirewe can use it and simply add functionality to make it moreefficient. The class you are going to be creating in thisassignment is called Random and it will be loosely based on theJava Random class.
Here is the functionality
Constructor Summary
- Random() - Default constructor
- Random(double min, double max); - generates randomnumbers between min and max.
- Random(Double min, Double max); - generates randomnumbers between min and max using class Double
- Random(int seed); - seed the random numbergenerator
Function Summary
- int nextInt() - Returns the next random number as anint
- Integer nextInteger() - Returns the next random number as anInteger class
- double nextDbl() - Returns the next random number as aprimitive double
- Double nextDouble() - Returns the next random number as aDouble class
- void setRange(double min, double max) - Sets the rage andrecreates random numbers
- void setRange(Double min, Double max) - Sets the range andrecreates the random numbers.
Specifics
Basically, this is a wrapper around the function rand. You willput a vector in your data section to hold 250 primitive doublevalues. When one of the constructors is called you will clear thevector and fill it with new random doubles. For this you shouldhave a private function called fillVect which will generate randomdoubles in whatever range is specified.. Once the vector isfilled you will want to shuffle the values in the vector around. Iwill leave it up to you to determine how to shuffle the vector butit may be easy to simply swap values at two random indexes a bunchof times. This function should be called shuffle and should bedefined as private.
To generate random doubles in a range you can use the followingalgorithm:
double r = (((double) rand() / (double) RAND_MAX) * (max- min)) + min ;
Where min and max are double values passed into theprivate function called fillVect. RAND_MAX is a constant that isadded when you include iostream. You do not need to do anything touse it.
Constructors
- The default constructor and the constructor that takesthe seed should simply fill the vector with numbers in the range of0 to RAND_MAX.
- All constructors except the constructor that takes the seedwill use the time function to seed the random numbergenerator.
- The constructor that takes seed should pass the value to srandfor seeding.
Functions
The next functions should return the next value in the vector aswhatever type specified.
Please note that when you have gone through 90% or more of thevector you should reshuffle and start from the beginning
The setRange functions should clear the vector and generate newrandom numbers for the vector. The vector should also be shuffledonce it has been filled.
Finally, try to avoid using literal values. You know thatyou should use 250 numbers and you need to reshuffle at 90%. Itwould be best to use constants define these values.
Step by Step Solution
3.39 Rating (161 Votes )
There are 3 Steps involved in it
Step: 1
Class Random include include include class Random private const int VECTORSIZE 250 const int RESHUFFLETHRESHOLD 90 stdvector vect double min double ma...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started