Question : The rand() function generates values on the basis of a distribution called the normal
distribution. One of the favorable properties of the normal distribution is that its average is exactly the
middle value of its range. It means that if we run the rand() function in C++, having the range 0 - 32767,
a large number of times and calculate the average of all the generated values, it would be exactly
16383.5. In this problem, you are required to find the smallest value of n (number of times the rand() is
called) that results in the average equal to 16383.5 +- 0.0001. Specifically, your program shall call rand()
1000 times and see the average, then call it another 10000 times and see the average, and so on until
the average is 16383.5 +- 0.0001.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
double x,y,c,n=1;
while(n<=1000){
x = (rand() % (32767 + 1 ) + 0) ;
y = x/n;
if((y = (16383.5+0.0001))||(y = (16383.5-0.0001))){
break;
}
n++;
}
cout<<n<<endl<<y<<endl;
}
|
I just wanna ask how this code will pursue. In simple I want to understand a logic if someone of you got it better.