Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help me with this question? it uses C++ and the header file below is

ID: 3847572 • Letter: C

Question


Can anyone help me with this question? it uses C++ and the header file below is used to call rx_normal() function.

5. Verify the Normal Random Number You are asked to veri the rx normal function whether it generates Gaussian random numbers. Call the rx normal (u, s) for n times where u and s are the desired mean and standard deviation, respectively. calculate their sum and sum of square as yx and Ex2 and compare their real mean and standard deviation to the desired mean and standard deviation. E[X] E[X2] Verify the ratio of samples within u to, u t 20, t 3o ranges based on the statistics from the wikipedia https n wiki wiki Norm distribution).

Explanation / Answer

Answer: You have only specified the header(.h) file. But to use function actually, .cpp file containg definition of function is required. So I have defined my own function to generate random normal numbers to show the working of program. In place of this, you can use your own function i.e. rx_normal().

See the code below:

------------------------------------------

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

double random_normal(double mean, double sd)
{
double min, max, sqrd, mult;
static double temp1, temp2;
static int seed = 0;
if (seed == 1)
    {
      seed = !seed;
      return (mean + sd * (double) temp2);
    }
do
{
     min = -1 + ((double) rand () / RAND_MAX) * 2;
     max = -1 + ((double) rand () / RAND_MAX) * 2;
     sqrd = pow (min, 2) + pow (max, 2);
}while (sqrd >= 1 || sqrd == 0);
mult = sqrt ((-2 * log (sqrd)) / sqrd);
temp1 = min * mult;
temp2 = max * mult;
seed = !seed;
return (mean + sd * (double) temp1);
}

int main()
{
int N = 10;
double *elems = new double[N];
double mean=5.0,sd=2.0;
cout<<"Generated numbers are:"<<endl;
for(int i=0;i<N;i++)
{
      elems[i]=floor(random_normal(mean,sd)*100)/100;
      cout<<elems[i]<<endl;
}
double sum=0.0,sum_sqrd=0.0;
for(int i=0;i<N;i++)
{
      sum+=elems[i];
      sum_sqrd+=pow(elems[i],2);
}
double mean_N=sum/N;
double mean_N_sqrd=sum_sqrd/N;
double sigma = sqrt(mean_N_sqrd - pow(mean_N,2));
cout<<"desired mean:"<<mean<<", desired standard deviation:"<<sd<<endl;
cout<<"real mean:"<<mean_N<<", real standard deviation:"<<sigma<<endl;
int> for(int i=0;i<N;i++)
{
      double number=elems[i];
      if (number >= (mean_N-sigma) || number <= (mean_N+sigma))
      {
          one_sigma_range++;
      }
      else if (number >= (mean_N-2.0*sigma) || number <= (mean_N+2.0*sigma))
      {
          two_sigma_range++;
      }
      else if (number >= (mean_N-3.0*sigma) || number <= (mean_N+3.0*sigma))
      {
          three_sigma_range++;
      }
}
cout<<"Proportion of numbers in one sd range:"<<(one_sigma_range/N)*100<<endl;
cout<<"Proportion of numbers in two sd range:"<<(two_sigma_range/N)*100<<endl;
cout<<"Proportion of numbers in three sd range:"<<(three_sigma_range/N)*100<<endl;
return 0;
}

---------------------------------

Output:

-----------------------

Generated numbers are:
3.94
6.05
10.14
3.78
4.42
6.52
6.44
6.1
4.52
5.52
desired mean:5, desired standard deviation:2
real mean:5.743, real standard deviation:1.76195
Proportion of numbers in one sd range:100
Proportion of numbers in two sd range:0
Proportion of numbers in three sd range:0

-----------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote