Thread: hrm?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    hrm?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include "gslrng.h"
    
    int main(int argc, char *argv[])
    {
      int N,i;
      double Xo, mean, sigma, variance, *a, u;
      
      {
      if(argc != 4) 
        {
          printf("Error!\nUsage:N(int) Xo(double) sigma(double)\n", argv[0]);
          return 1;
        }
      else
        N = atoi( argv[1] );
      }  
    
      Xo = atof( argv[2] );
      sigma = atof( argv[3] );
    
        const gsl_rng_type * T;
        gsl_rng * r;
    
        gsl_rng_env_setup();
        
        T = gsl_rng_default;
        r = gsl_rng_alloc (T);
        
        a = (double *) malloc (N * sizeof *a);
           
        printf ("%d %lf %lf\n", N, Xo, sigma);  
        
        for (i = 0; i < N; i++)
          {
          double u  = gsl_ran_gaussian (r,sigma);
          a[i]=u;	
          printf ("%lf\n", a[i]);
          }
    
        free(a);
        
        gsl_rng_free (r);
    
        return 0;
    }
    The outcome should be an array consist of randomly generated numbers from gsl_ran_gaussian generator...what's wrong with the code becuase it doesn't seem to print out random numbers at all.?
    Last edited by vutek0328; 08-28-2006 at 04:14 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop printing a and print a[ i ]. Oh, and pay attention to your compiler's warnings once in a while.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > N = atoi( argv[1] );
    This is part of the else statement, the rest of the code isn't.
    Use braces to make your intentions clear.

    > printf ("%lf\n", a);
    If you want to print array elements, rather than just the base address of the array in a very wrong format, try
    printf ("%lf\n", a[i] );

    > a = (double *) malloc (N * sizeof(double));
    Read the FAQ entry on casting malloc in C.
    a = malloc ( N * sizeof *a );
    is the preferred form.

    > gsl_rng * r;
    Standard C doesn't support mixing declarations and statements.

    I'm guessing you're using gcc as your compiler?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote Originally Posted by Salem
    > gsl_rng * r;
    Standard C doesn't support mixing declarations and statements.
    I took this piece of code that generates random numbers from the gsl random generator website, I tried to ask my mentor but he said he doesn't understand completely what each part of the code does either, so he told me just to copy everything and only make the necessary changes.

    What can I do to fix this part?

    Quote Originally Posted by Salem
    I'm guessing you're using gcc as your compiler?
    Correct.

    I updated the code with the additions, but when I run it, no matter what changes I make to N, Xo and sigma, the same random numbers always comes out?
    Last edited by vutek0328; 08-28-2006 at 04:16 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Post your latest code.

    To enable warnings with GCC, use this:
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 other-arguments
    One of the warnings should be about this line:
    Code:
    printf("Error!\nUsage:N(int) Xo(double) sigma(double)\n", argv[0]);
    The argv[0] doesn't match anything in the format string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
    /*Monte Carlo Simulation with gaussian random number generator*/
    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    
    int main(int argc, char *argv[])
    {
      int N,i;
      double Xo, mean, sigma, variance, *a;
      
      {
      if(argc != 4) 
        {
          printf("Error!\nUsage:N(int) Xo(double) sigma(double)\n");
          return 0;
        }
      else
        N = atoi( argv[1] );
      }  
    
      Xo = atof( argv[2] );
      sigma = atof( argv[3] );
    
        const gsl_rng_type * T;
        gsl_rng * r;
    
        gsl_rng_env_setup();
        
        T = gsl_rng_default;
        r = gsl_rng_alloc (T);
        
        a = (double *) malloc (N * sizeof *a);
           
        printf ("%d %lf %lf\n", N, Xo, sigma);  
        
        for (i = 0; i < N; i++)
          {
          double u = gsl_ran_gaussian(r,sigma);
          a[i]=u;	
          printf ("%u\n", a[i]);
          }
    
        int total = 0, X;
        int add_array(double a[i],int  N);
          {
    	int i;
    	
    	for(i = 0; i < N; i++)
    	  total += a[i];
    	X += a[i] * a[i];
          }
          mean = total/N;
          variance = X/N;
          printf ("\nTotal:%d\nMean:%lf\nVariance:%lf\n", total, mean,variance);
    	
        free(a);
        
        gsl_rng_free (r);
    
        return 0;
    }
    MCgsl.c:41: warning: implicit declaration of function `gsl_ran_gaussian'
    MCgsl.c:43: warning: unsigned int format, double arg (arg 2)
    MCgsl.c:46: warning: `X' might be used uninitialized in this function

    I already fixed some of the other warnings I got, but I don't understand what the remaining warnings want me to fix, I suspect warning on line 43 has something to do with %u...

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    MCgsl.c:41: warning: implicit declaration of function `gsl_ran_gaussian'
    You don't have a prototype for the mentioned function before the function's first use. Maybe you need another header file?
    Code:
    MCgsl.c:43: warning: unsigned int format, double arg (arg 2)
    ...I suspect warning on line 43 has something to do with %u...
    Yes, it does; you're passing a double and %u expects an unsigned int. Use %f or pass it what it expects.
    Code:
    MCgsl.c:46: warning: `X' might be used uninitialized in this function
    X is used without a value being assigned to it:
    Code:
        int total = 0, X;
        int add_array(double a[i],int  N);
          {
    	int i;
    	
    	for(i = 0; i < N; i++)
    	  total += a[i];
    	X += a[i] * a[i];
          }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    w00t, all the warning are fixed now

    so now when I execute the code, the following things occur:

    1) ./MCgsl.exe 25 1 0.5
    If I enter that command line several times, am I supposed to get a different list of randomly generated numbers or the same?

    2) Why does the sum of all the randomly generated integers = 0 (likewise with mean and variance?) regardless of what parameters I put in?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >the same random numbers always comes out?

    > r = gsl_rng_alloc (T);
    At this point could seed the random number generator, maybe using the time function or something similar:
    Code:
    gsl_rng_set (r, time(NULL));
    Or alternately there's an environment variable you can set.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    total += a[i];
    Since total is an int, adding numbers < 1 to it would be like adding zero to it over and over again. Use a double.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    Talking

    Quote Originally Posted by swoopy
    >the same random numbers always comes out?

    > r = gsl_rng_alloc (T);
    At this point could seed the random number generator, maybe using the time function or something similar:
    Code:
    gsl_rng_set (r, time(NULL));
    Or alternately there's an environment variable you can set.
    This was part of the code that my mentor said he didn't understand himself, so naturally I just left it alone. First of all, I'm curious to know what exactly this part does and secondly, when I use the method you suggested, the following error comes up. As from previous explained by dwks, r is used without a value being assigned to it. I am unsure of what it is supposed to be assigned to, since it doesn't work if you assign it to 0 lol

    MCgsl.c:29: warning: `r' might be used uninitialized in this function

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
        int add_array(double a[i],int  N);
          {
    	int i;
    	
    	for(i = 0; i < N; i++)
    	  total += a[i];
    	X += a[i] * a[i];
          }
    Are you trying to make a function here? If so, put it outside main() and remove the red semicolon. And probably declare X and add return X.

    Code:
    MCgsl.c:29: warning: `r' might be used uninitialized in this function
    Uninitialized warnings just mean that you've read from the variable before you set it to something, like this:
    Code:
    int x;
    printf("%i\n", x);
    You can usually fix them by assigning a value to the variable in question.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I tried to ask my mentor but he said he doesn't understand completely what each part of the code does either
    You need a better mentor then if they can't figure out those few lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    Cool

    He's not a C-specialist, his knowledge of C is pretty deep, but not all-knowning like you fellas

    Thanks for all your help!

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    
    int main(int argc, char *argv[])
    {
      int N,i;
      double Xo, mean, sigma, variance, *a;
      
      {
      if(argc != 4) 
        {
          printf("Error!\nUsage:N(int) Xo(double) sigma(double)\n");
          return 0;
        }
      else
        N = atoi( argv[1] );
      }  
    
      Xo = atof( argv[2] );
      sigma = atof( argv[3] );
    
        const gsl_rng_type * T;
        gsl_rng * r;
    
        gsl_rng_env_setup();
        
        T = gsl_rng_default;
        r = gsl_rng_alloc (T);
        
        a = (double *) malloc (N * sizeof *a);
           
        printf ("%d %lf %lf\n", N, Xo, sigma);  
        
        for (i = 0; i < N; i++)
          {
          double u = gsl_ran_gaussian(r,sigma);
          a[i]=u;	
          printf ("%u\n", a[i]);
          }
    Why is it, that when I change the Xo value, it still generates numbers from -1 to 1? *Does it have something to do with the random number generator itself? as in, it only generates [-1, 1]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game file patch program...
    By RoD in forum Game Programming
    Replies: 13
    Last Post: 01-28-2003, 10:44 AM
  2. should I get a job as a programmer, or join the air force?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 01-22-2003, 12:53 AM
  3. Replies: 2
    Last Post: 01-18-2003, 01:32 AM