Noise signals
In engineering simulations, we often want to generate a floating point sequence of values with a specified mean and variance. The function developed in this chapter allows us to generate numbers between limits a and b but it does not allow us to specify the mean and variance. By using results from probability, the following relationships can be derived between the limits of a uniform random sequence and its theoretical mean and variance.
Variance=((b-a)^2)/12 , Mean=(a+b )/2
Write a function named rand_mv that generates a random floating point value with a specified mean and variance that are input parameters to the function. Assume the the corresponding function prototype is double rand_mv(double mean, double var);
The problem also says use the rand_float function developed in chapter 5. This function is typed out below.
/*----------------------------------------------------------------------------------------------------------------------------*/
/* This function generates a random double value between a and b. */
double rand_float(double a, double b )
{
return ((double)rand()/RAND_MAX)*(b-a)+a;
}
/*-------------------------------------------------------------------------------------------------------------------------------*/
This is the new program I came up with and it seems right but spits out a garbage floating pt value
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
double rand_mv(double var, double mean);
int main(int argc, char *argv[])
{ unsigned int seed;
double var, mean, a, b;
cout << "Enter a positive integer seed value ";
cin >> seed;
srand(seed);
cout << "Enter positive variance and mean seperated by a space ";
cin >> var >> mean;
cout << "var=" << var;
cout << "mean=" << mean;
a=(2*mean)-(sqrt(12)*sqrt(var));
b=mean+((sqrt(12)*sqrt(var))*.5);
cout << "a=" << a;
cout << "b=" << b;
cout << "Random floating pt value is " << rand_mv(var, mean);
system("PAUSE");
return EXIT_SUCCESS;
}
double rand_mv(double var, double mean)
{
double a, b; //call a and b values from main to user defined function
return ((double)rand()/32767)*(b-a)+a;
}
suggestions anyone??????

New Topic/Question
Reply



MultiQuote



|