COSMOS v7.655  COSMOSv7655
(AirShowerMC)
knbino.f
Go to the documentation of this file.
1 ! include 'rnd.f'
2 !--------------------------------
3 !c to test sampling from the negative binomial dist.
4 ! implicit none
5 ! real*8 k, aven
6 ! integer n, i
7 ! aven = 10.
8 ! k = 1.5
9 ! do i=1,10000
10 ! call knbino(k, aven, n)
11 ! write(*, *) float(n), float(n)/sngl(aven)
12 ! enddo
13 ! end
14  subroutine knbino(k, avn, n)
15 ! samples n from the negative binomial distrubtion of
16 ! p(n)= (n+k-1)] / n]/(k-1)] ( a/(1+a) )** n / (1+a)**k
17 ! where a=avn/k. k should be real*8
18 ! this is for unfixed k and avn. If either or both of them are
19 ! fixed, user anothr method for faster sampling.
20 !
21  implicit none
22  real*8 k, avn
23  integer n
24 !
25  real*8 a, pn, ar, u, sum
26  integer nmax
27 !
28  a=avn/k
29  pn= 1.d0/(1.d0+a)**k
30  ar=a/(1.d0+a)
31  call rndc(u)
32  sum=pn
33  nmax=avn*15.d0
34  n=0
35  do while (sum .lt. u .and. n .lt. nmax)
36  n=n+1
37  pn= pn* (n+k-1)/n * ar
38  sum=sum+pn
39  enddo
40  end
subroutine rndc(u)
Definition: rnd.f:91
block data cblkEvhnp ! currently usable models data RegMdls ad *special data *Cekaon d0
Definition: cblkEvhnp.h:5
subroutine knbino(k, avn, n)
Definition: knbino.f:15