(Random Sequence) Create a class that returns a random number from the following
ID: 3660459 • Letter: #
Question
(Random Sequence) Create a class that returns a random number from the following set, {16,34,57,79,121}. Loop 100,000 times with this procedure and print the frequency of each of the 5 numbers obtained. The following is the specification for the class. Specification class Prob1Random { private: char *set; //The set of numbers to draw random numbers from char nset; //The number of variables in the sequence int *freq; //Frequency of all the random numbers returned int numRand; //The total number of times the random number function is called public: Prob1Random(const char,const char *); //Constructor ~Prob1Random(void); //Destructor char randFromSet(void); //Returns a random number from the set int *getFreq(void) const; //Returns the frequency histogram char *getSet(void) const; //Returns the set used int getNumRand(void) const; //Gets the number of times randFromSet has //been called }; Driver program to return a random sequence char n=5; char rndseq[]={16,34,57,79,121}; int ntimes=100000; Prob1Random a(n,rndseq); for(int i=1;i<=ntimes;i++) { a.randFromSet(); } int *x=a.getFreq(); char *y=a.getSet(); for(int i=0;i<<int(y[i])<<" occured "<<x[i]<<" times"<<endl; } cout<<"The total number of random numbers is "<<a.getNumRand()<<endl; Sample Output 16 occured 20045 times 34 occured 19952 times 57 occured 20035 times 79 occured 20039 times 121 occured 19929 times The total number of random numbers is 100000 Note: Your results are not expected to be exactly the same! After all these are pseudo-random number sequences with different seeds.Explanation / Answer
by imp[oring the given class in code we can get to the results
#include <iostream>#include <cmath>#include <limits>int main(){ srand(time(0)); //seeds the rand() function const int N = 20; int random_number = 0; int largest = 0; int smallest = 200; int sum = 0; for (int i = 0; i < 20; i++) //for int i = 0; i is smaller than 20 (will stop when i is 19), i + 1 { random_number = rand() % 200 + 1; //random number is now 1 - 200 (0 - 199 + 1) std::cout << "Random number " << i << ": " << random_number << std::endl; //prints random_number sum += random_number; //sum = sum + random_number if (random_number > largest) //if random_number is bigger than largest largest = random_number; //its now largest else if (random_number < smallest) //else if its smaller than smallest smallest = random_number; //its now smallest } std::cout << "Largest number is: " << largest << std::endl //prints largest << "Smallest number is: " << smallest << std::endl //prints smallest << "Average is: " << sum / N << std::endl; //prints average (sum divided by N which is const int 20) std::cout << "Press ENTER to continue..." << std::endl; std::cin.ignore(std::numeric_limit<std::streamsize>::max(), ' '); //a better way to System("PAUSE"); you need to #include <limits> for this return(0);} Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.