random number gen difficult

This is a discussion on random number gen difficult within the C Programming forums, part of the General Programming Boards category; I have 2 pieces of code called GENERO1.c and gauss2.c which will generate two random numbers in Polar marsaglia method. ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    random number gen difficult

    I have 2 pieces of code called GENERO1.c and gauss2.c which will generate two random numbers in Polar marsaglia method. However, something is not working properly because when I run another code where I call the function gauss2() , its stopping in that function like being in an infinite loop. Could anyone tell me why is not working?
    Bellow is the code?
    Please I would welcome any help, because I really cant find something.

    I compile the program with:
    gcc -o check -lm gauss2.c GENER01.c check.c
    and run by: ./check

    GENER01.c
    Code:
     
    /* NOTE works for 32 bit !!!!!!!!!! use  "-m32" */
    #include <stdio.h>
    #include<math.h>
    #define znew (z=36969*(z&65535)+(z>>16))
    #define wnew (w=18000*(w&65535)+(w>>16))
    #define MWC ((znew<<16)+wnew )
    #define SHR3 (jsr^=(jsr<<17), jsr^=(jsr>>13), jsr^=(jsr<<5))
    #define CONG (jcong=69069*jcong+1234567)
    #define FIB ((b=a+b),(a=b-a))
    #define KISS ((MWC^CONG)+SHR3)
    #define LFIB4 (c++,t[c]=t[c]+t[UC(c+58)]+t[UC(c+119)]+t[UC(c+178)])
    #define SWB (c++,bro=(x<y),t[c]=(x=t[UC(c+34)])-(y=t[UC(c+19)]+bro))
    #define UNI (KISS*2.328306e-10)
    #define VNI ((long) KISS)*4.656613e-10
    #define UC (unsigned char) /*a cast operation*/
    typedef unsigned long UL;
    /* Global static variables: */
    static UL z=362436069, w=521288629, jsr=123456789, jcong=380116160;
     static UL a=224466889, b=7584631, t[256];
    /* Use random seeds to reset z,w,jsr,jcong,a,b, and the table
    t[256]*/
    /*static UL x=0,y=0,bro; static unsigned char c=0;*/
    /* Example procedure to set the table, using KISS: */
    void settable(UL i1,UL i2,UL i3,UL i4,UL i5, UL i6)
    { int i; z=i1;w=i2,jsr=i3; jcong=i4; a=i5; b=i6;
    for(i=0;i<256;i=i+1) t[i]=KISS;
    }
    
    int Threepoint(){
      return(KISS%3);
    }
    int Eightpoint(){
      return(KISS%8);
    }
    int Fourpoint(){
      return(KISS%4);
    }
    
    double GENER02(double lambda){
      /* lambda > 0 */
      /* 
         function is a translation from P.E. Kloeden E. Platen and H. Schurz 
         Numerical Solution of SDE Trough Computer experiments, Springer 1994.
         page 11.
      */
      double xu;
      double Uniform();
      xu=Uniform();
      if(xu>0.0)
        xu=-log(xu)/lambda;
      else
        xu=0.0;
      return(xu);
    }     
    
    double Uniform(){
      return((double) UNI);
    }
    
    double GENER01(double sqrtDT){
      /*  *WT=-sqrtDT+2*(KISS%2)*sqrtDT; */
      if(KISS%2)
        return(-sqrtDT);
      else
        return(sqrtDT);
    }
    
    double GENER03(double sqrt3DT){
      long I;
      double Y;
      /* generate a random variable on set {1,2,3,4,5,6} with uniform prob. */
    
      I = KISS%6+1; /* six point random variable 1, 2,...,6.
    		   Each element prob. 1/6 */
      if(I>2) /* prob. 2/3 */
        Y=0.0;
      else{
        if(I==1) /* prob. 1/6 */
          Y=-sqrt3DT;
        else    /* prob. 1/6 */
          Y=sqrt3DT;
      }
      /*  printf("%g  %d\n",Y,I); */
      /*  printf("%g\n",Y); */
      return( Y );
    }

    gauss2.c


    Code:
    #include<math.h>
    
    /* Polar Marsaglia method for gaussian dist. pseudo-random no.generator*/
    
    void gauss2(double *z1,double *z2){
      double y, v1, v2,r, fac;
      double Uniform();
      double temp;
      r=100.0;
      while(r>1.0 || r<=0.0){
        y=Uniform();     /* rand. no [0:1] */
        v1 = 2.0*y - 1.0;                /* rand. no [-1:1] */
        y=Uniform();
        v2 = 2.0*y - 1.0;                /* rand. no [-1:1] */
        r = v1 * v1 + v2 * v2; 
        }
      fac = (sqrt( -2.0*log(r) / r));
      *z1 = v1 * fac;
      *z2 = v2 * fac; 
      return;
    }
    check.c

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    typedef unsigned long UL;
    #define pi 3.14159
    double Uniform();
    void gauss2(double *z1,double *z2);
    
    
    main()
    
    {
    
      int i;
      int j=100;
      int new;
    
     double z1, z2;
    
     UL i1, i2,i3,i4,i5,i6; /* to initialize Uniform (AND gauss2) */
        i1=time(NULL); i2= i1+23;i3= i1+243;i4= i1+26;i5= i2+28;i6= i1+27;
        settable(i1,i2,i3,i4,i5,i6);
    
        for(i=0;i<j;i++)
          {
    	printf("OK\n");
      gauss2(&z1,&z2);
      printf("%lf",z1);
          }
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hard to say. If it stops in that function then you should use a gdb or another debugger to inspect the values of the variables that are used in the while condition up to that point and during the loop. The infinite loop is obviously due to the fact that the loop condition is never false.

    Alternatively, you can just use plenty of printfs to see what values those variables get.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Not only hard to say, it's also hard to read!
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    random number gen difficult

    please see the fpodt with the same number.

    I didn't find why is not working It prints the OK but not the random number

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If I run that code I get
    Code:
    $ ./check
    OK
    -0.121324OK
    0.060977OK
    0.129352OK
    -1.588561OK
    -0.079074OK
    -0.820470OK
    -0.651844OK
    1.143829OK
    -1.202794OK
    -0.252865OK
    0.601867OK
    -0.950832OK
    -0.415677OK
    -1.145437OK
    1.959113OK
    -0.905978OK
    -0.959364OK
    -0.386680OK
    0.835786OK
    1.298417OK
    1.103815OK
    1.244459OK
    0.514370OK
    -1.742448OK
    -0.956206OK
    0.258660OK
    -1.325918OK
    0.566547OK
    0.101843OK
    0.741819OK
    -0.367660OK
    -0.475930OK
    -0.752620OK
    -0.555959OK
    -0.862904OK
    -0.894565OK
    1.485519OK
    1.112904OK
    -0.514475OK
    0.981045OK
    0.463184OK
    0.721194OK
    0.798182OK
    -0.328830OK
    -1.107571OK
    2.585801OK
    0.769873OK
    -0.942267OK
    -0.961299OK
    -0.277848OK
    0.994247OK
    -1.095895OK
    0.805629OK
    -1.336201OK
    -0.366741OK
    0.055287OK
    1.015542OK
    -0.923245OK
    -0.053370OK
    -0.344773OK
    -0.025563OK
    -1.504146OK
    -0.913850OK
    -0.406136OK
    -0.674365OK
    0.317095OK
    0.016368OK
    -0.345860OK
    -0.188618OK
    -0.900776OK
    -0.432226OK
    0.154195OK
    -0.359207OK
    0.982756OK
    0.133604OK
    -0.382011OK
    0.242919OK
    -1.346628OK
    0.005299OK
    -0.117813OK
    0.446642OK
    0.015459OK
    -0.751990OK
    0.055946OK
    -0.058842OK
    0.944983OK
    -1.257753OK
    -1.553232OK
    1.037597OK
    -1.463427OK
    -1.189913OK
    0.687363OK
    0.922469OK
    -0.547706OK
    -0.774758OK
    0.789586OK
    0.426197OK
    0.764199OK
    1.265462OK
    0.335893
    Is that not what you get?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    random number gen difficult

    no how you did that? Please help

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    random number gen difficult

    How did you compile? It prints the first ok and then no number it keeps running without printing anything
    I couldnt debugg

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I copy-and-pasted your code, compiled it, decided the warnings were benign, and ran it. For reference, the warnings were
    Code:
    $ gcc -o check -Wall -Wextra gauss2.c GENER01.c check.c -lm
    gauss2.c: In function ‘gauss2’:
    gauss2.c:8: warning: unused variable ‘temp’
    GENER01.c: In function ‘GENER02’:
    GENER01.c:47: warning: implicit declaration of function ‘log’
    GENER01.c:47: warning: incompatible implicit declaration of built-in function ‘log’
    check.c:13: warning: return type defaults to ‘int’
    check.c: In function ‘main’:
    check.c:23: warning: implicit declaration of function ‘settable’
    check.c:17: warning: unused variable ‘new’
    check.c:31: warning: control reaches end of non-void function

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    Its not working at me so i guess is a matter of library missing or whatever. Many thanks

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    my warnings are :

    gcc -o check -Wall -Wextra gauss2.c GENER01.c check.c -lm
    gauss2.c: In function ‘gauss2’:
    gauss2.c:8: warning: unused variable ‘temp’
    check.c:13: warning: return type defaults to ‘int’
    check.c: In function ‘main’:
    check.c:22: warning: implicit declaration of function ‘settable’
    check.c:30: warning: control reaches end of non-void function


    si I guess something to do with GENERO1.c

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You might want to try fixing those errors -- the built-in ones tend to come out okay, but maybe settable is not getting set up correctly without a prototype. (And I can't think it will matter, but -lm really should be at the end IIRC.)

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    yes I am trying to compile exactly as the one that it worked but it prints ok and then the cursor flashes without going anywhere.

    Its not the settable I think

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    So try
    gdb ./check

    Then
    run

    Then wait for it to print your first result and "lock up"

    Then press ctrl-c to break in, then do a backtrace and start some analysis of where you are.
    Then say step the code to work out which loop it is stuck in.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    I know that I am stuck in the function call since even if I add a printf "something" after the function call nothing is printed. I ve never used gdb before so I need more info how to do if u think it can help

    MANY THANKS

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    Program received signal SIGINT, Interrupt.
    0x000000000040068e in gauss2 ()
    Current language: auto; currently asm
    Does anyone knows what happens?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  2. couple questions for very small random number program
    By nick753 in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2010, 08:35 PM
  3. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 09:46 AM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21