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.

So which part fo the code u dont understand well? Where did u get the code from?

ssharish

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.

looks like you havn't included the math.h header file. Do this at top

#include <math.h>

ssharish

>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

Member Avatar for iamthwee

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.