Question

Create and implement a C++ class, RandomGenerator, that: Allocates and stores an array of random floating...

Create and implement a C++ class, RandomGenerator, that:

  • Allocates and stores an array of random floating point numbers between 0 and 1.
  • The size of the array is passed into the constructor.
  • The class contains a next() function which returns a new random number from the array every time the next() function is called (without calling rand()).
  • Deletes any memory allocated on the heap when the class is destroyed.
  • Note: This trick pre-calculates random numbers and saves execution time by storing values in memory.
#include <iostream>
#include <cstdlib>
using namespace std;

Class RandomGenerator {
...
}

int main() {
    RandomGenerator random(100000);
    for (int i = 0; i < 4; i++) {
            cout << random.next() << endl;
    }
    return 0;
}

output:

0.394383
0.783099
0.79844
0.911647
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

#include <iostream>
#include <cstdlib>
using namespace std;

class RandomGenerator {
   private:
       double *p;
       int index;
   public:
       RandomGenerator(int size){
           p = new double[size];
           index = 0;
           for(int i=0; i<size;i++) p[i] = (double) rand() / RAND_MAX;
       }
       double next() {
           return p[index++];
       }
       ~RandomGenerator(){
           delete[] p; //Deletes any memory allocated on the heap
       }
};

int main() {
RandomGenerator random(100000);
for (int i = 0; i < 4; i++) {
cout << random.next() << endl;
}
return 0;
}

============================================================

Add a comment
Know the answer?
Add Answer to:
Create and implement a C++ class, RandomGenerator, that: Allocates and stores an array of random floating...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • C++ Write a program that generates and prints 24 random values using an array and the...

    C++ Write a program that generates and prints 24 random values using an array and the rand () function. Implement an algorithm to find the maximum and a minimum number of random numbers generated. Use the header "#include <ctime>", "#include <cstdlib>" in addition to the usual header: "#include <iostream>"

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...

  • REWRITE the code below and declare a class that holds an array of 10. Create a...

    REWRITE the code below and declare a class that holds an array of 10. Create a constructor that initializes the array with random integers 1 and 100. Write a member function to show the entire array and another to sort the array. Rewrite with instruction above: //Exchange Sort Function for Descending Order void ExchangeSort(vector<int> &num) {      int i, j;      int temp;   // holding variable      int numLength = num.size();      for (i=0; i< (numLength -1); i++)       {...

  • #include<iostream> #include<cstdlib> using namespace std; int main() {     // create array of size 20    ...

    #include<iostream> #include<cstdlib> using namespace std; int main() {     // create array of size 20     int arr[20];         int i;         cout<<"scores : ";     for( i = 0 ; i < 20 ; i++ )     {         // generate a random number i range 70 - 100 an add it to arr         arr[i] = rand() % 31 + 70;                 cout<<arr[i]<<" ";     }         // store the total score     int total = 0;...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • IN C++ Create a class to act as a generic array (i.e. the user will be...

    IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...

  • a. Write a function named findmax() that finds and displays the maximum values in a two...

    a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100. b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value. I have this so far, and but it's only finding the...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT