# Issue with Random Number Generation

This is a discussion on Issue with Random Number Generation within the C Programming forums, part of the General Programming Boards category; Hey everyone, Having some problems with generating a random number w. I'm using an algorithm to create w by generating ...

1. ## Issue with Random Number Generation

Hey everyone,

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:

Thanks once again guys! XD

2. What would you consider a "sensible answer" for w?

3. Originally Posted by sam.briggs
Hey everyone,

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;
}```
Thanks once again guys! XD
The '%f' format specifier in scanf expects the address of a float, not a double. Fix that first and then come back if it still doesn't work as expected.

4. Cheers guys that sorted it out! XD

Popular pages Recent additions