Having some problems with generating a random number w. I'm using an algorithm to create w by generating two numbers between -1 and 1 and then squaring them and then summing then and then using user in putted means and standard deviations to generate tailor made distributions of random numbers. Here it is: NO code please just advice on what I'm doing wrong as if you run it it never produces sensible answers for w:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double x, y, z, m, s, w; //Define set of doubles for data storgae
int i, N; //Define Integer for counting purposes
printf("Please input the standard deviation you require:"); //Acquire sigma data
scanf("%f",&s);
printf("\nPlease input the mean value you require from the Gaussian distribution:"); //Acquire mu data
scanf("%f",&m);
printf("\nPlease state the number of random variables you would like printed:");
scanf("%d",&N);
for(i=0; i<N; i++) //Generate random variable z between 0 and 1
{
x = 2* rand()/(double)RAND_MAX -1.0;
y = 2* rand()/(double)RAND_MAX -1.0;
z = (x*x) + (y*y);
if(z<1)
{
w = ((s*x*sqrt((-2.0*log(z))/(double)z))+m);
printf("x=%f, y=%f, z=%f, w=%f\n",x,y,z,w);
}
else
{
printf("Z is invalid=%f, x=%f, y=%f\n",z,x,y);
}
}
return 0;
}