home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / C-SSP.ARJ / PORNG.C < prev    next >
Encoding:
Text File  |  1984-08-10  |  974 b   |  41 lines

  1.    porng(l,max,iflag,r,y,s,x)
  2.  
  3.       /* this function generates a sequence of random numbers from    */
  4.       /* uniformly distributed random numbers by the method of linear */
  5.       /* approximation of the inverse of the accumulative Poisson     */
  6.       /* distribution function.                                       */
  7.  
  8.       int iflag,l,max;
  9.       float *r,*x,s[],y[];
  10.  
  11.    {
  12.       int i,n;
  13.       float e,r1,xl,z;
  14.       extern double pow();
  15.  
  16.       if (iflag != 0)
  17.       {
  18.        e = .367879;
  19.        iflag = 0;
  20.        y[0] = 0.0;
  21.        xl = l;
  22.        z = pow(e,xl);
  23.        n = max - 1;
  24.        y[max] = 1.0;
  25.  
  26.        for(i = 1; i <= n; i++)
  27.        {
  28.        y[i] = y[i-1] + z;
  29.        s[i-1] = y[i] - y[i-1];
  30.        z = z * xl / i;
  31.        }
  32.       s[max-1] = 1. - y[max-1];
  33.       }
  34.       r1 = *r;
  35.       strnum(&r1);
  36.       *r = r1;
  37.       for(i = 1; i <= max; i++)
  38.        if(*r <= y[i]) break;
  39.       *x = (*r - y[i-1])/s[i-1] + i;
  40.    }
  41.