2 Replies - 4236 Views - Last Post: 16 October 2009 - 02:37 PM Rate Topic: -----

#1 joemike1016   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 09

noise signals

Posted 16 October 2009 - 01:11 PM

Here is my assigned problem
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??????

Is This A Good Question/Topic? 0
  • +

Replies To: noise signals

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: noise signals

Posted 16 October 2009 - 01:21 PM

You need to read up on functions, you don't appear to understand them.
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;
}


Your comment is incorrect. a and b inside this function are uninitialized local values which are created on entering the function and disappear on leaving it. They are full of random data, hence your garbage value. It looks like you want to use the passed var and mean variables.
Was This Post Helpful? 0
  • +
  • -

#3 joemike1016   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 09

Re: noise signals

Posted 16 October 2009 - 02:37 PM

View PostJackOfAllTrades, on 16 Oct, 2009 - 12:21 PM, said:

You need to read up on functions, you don't appear to understand them.
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;
}


Your comment is incorrect. a and b inside this function are uninitialized local values which are created on entering the function and disappear on leaving it. They are full of random data, hence your garbage value. It looks like you want to use the passed var and mean variables.
. Ok I understand now that the a and b variables are incorrectly called from my main so how do I correctly call them and get this thing up and running? Thanks for your time and looking at myprogram for me by the way
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1