WRITE A C++ PROGRAM TO: Generate 200 numbers between 0 and 50. Create a frequenc
ID: 3688452 • Letter: W
Question
WRITE A C++ PROGRAM TO:
Generate 200 numbers between 0 and 50.
Create a frequency distribution by counting the number of instances a generated number falls between 0 – 10, 11 – 20, 21 – 30, 31 – 40, 41 – 50.
Do you think the random # generator is truly random? If so, why. If not, why not.
Run the random # generator again, but this time 200 numbers between -50 and 0. Take the absolute value of each of the results and run the same analysis as you did above. (min, max, average, frequency distribution).
“Cout” the answer to the following questions: Should the results be similar? Were they or were they not? Why?
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
enum { SIZE = 200, LIMIT = 50 };
int counts[LIMIT] = {};
std::srand( ( unsigned int )std::time( nullptr ) );
for ( int i = 0; i < SIZE; ++i ) ++counts[ std::rand() % LIMIT ];
for ( int i : counts )
{
std::cout << i << " occurs " << counts[i] << " times ";
}
return 0;
}
Truely speaking, the numbers wont be random in nature as we have used the counter which will be just increasing the count.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
enum { SIZE = 200, LIMIT = -50 };
int counts[LIMIT] = {};
std::srand( ( unsigned int )std::time( nullptr ) );
for ( int i = 0; i < SIZE; ++i ) ++counts[ std::rand() % LIMIT ];
for ( int i : counts )
{
std::cout << i << " occurs " << counts[i] << " times ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.