I am new to programming in C and I'm trying to write a random number generator with a Gaussian distribution with user-defined mean and variance. I've looked at the code given in "Numerical Recipes in C" but I don't understand it very well. If anyone could point me in the direction of a tutorial or any other help it would be greatly appreciated.

When compiling the code given in "Numerical Recipes In C" I get undefined refrecence errors relating to 'main' and the log and sqrt functions. I'm not sure why I'm getting these. I am using gcc in Linux.

>I get undefined refrecence errors relating to 'main' and the log and sqrt functions. I'm not
>sure why I'm getting these. I am using gcc in Linux.
I'll make an educated guess and say that you didn't link with the math library:

$ gcc main.c -lm
$ ./a.out

I have included math.h. I ran into the same problem when using acos to define pi in another program. When I replaced acos with atan it compiled and worked correctly.

well, did u try Narue's solution, that could solve the problem as well.

That way you link the library directly to the object file of the code at linking time. And make sure the -lm should be at the end.

ssharish

I have included math.h. I ran into the same problem when using acos to define pi in another program. When I replaced acos with atan it compiled and worked correctly.

Strange?

#include <stdio.h>
#include <math.h>
#include <string.h>

int main ( void )
{
  float t = acos( 0.3 );
  printf( "%f",t );
  return 0;
}

output

thwee@thwee-desktop:~$ gcc -Wall pedantic.c -lm
thwee@thwee-desktop:~$ ./a.out
1.266104
This article has been dead for over six months. Start a new discussion instead.