COSMOS v7.655  COSMOSv7655
(AirShowerMC)
kbetar.f
Go to the documentation of this file.
1 !c for test of kbetar generator
2 ! implicit none
3 ! real*8 a, b, x
4 ! integer i
5 ! call cerrorMsg('Enter a, b', 1)
6 ! read(*, *) a, b
7 ! do i=1, 100000
8 ! call kbetar(a, b, x)
9 ! write(*, *) sngl(x)
10 ! enddo
11 ! end
12 !- -------------------------- for drawing curve ---------------
13 !
14 ! anrm=gamma(a)*gamma(b)/gamma(a+b)
15 !
16 ! f= x**(a-1.)* (1.-x)**(b-1.)/anrm
17 ! **************************
18  subroutine kbetar(a, b, x)
19 ! **************************
20  implicit none
21  real*8 a ! input. see below
22  real*8 b ! input. see below
23  real*8 x ! output. sampled random variable
24 
25 ! generate a random variable x following beta function kernel
26 ! i.e. density function is x**(a-1)* (1-x)**(b-1)dx
27 !
28 ! both of a and b cannot be < 1.0
29 ! if a and b unbalance largely, rejection rate will
30 ! become large: (a=2, b=5--->1/6.6 is accepted
31 ! a=2.1,b=1.2-->30 % rejection)
32 ! To sample 100000 variables, 4.226 sec is needed.
33 ! (wiht a=1.5, b=2.5 with facom m780)
34 ! this is sometimes faster than ksbeta for a class of a,b
35 ! but has limitations.
36 
37  real*8 ai, bm, u, bi, am
38 
39  if(a .lt. 1. and. b .lt. 1.) then
40  write(*,*) ' kbetar cannot accept a<1,b<1:'
41  write(*,*) ' use, ksbeta in such case'
42  write(*,*) ' a=',a, ' b=',b
43  stop
44  endif
45  if( (a .ge. b .and. b .ge. 1.) .or. a .lt. 1.) then
46  ai=1./a
47 
48  bm=b-1.
49 ! *** until loop***
50  do while (.true.)
51  call rndc(u)
52 
53  x=u**ai
54  call rndc(u)
55  if ( u .le. (1.-x)**bm)
56  * goto 100
57  enddo
58  100 continue
59  else
60  bi=1./b
61 
62  am = a-1.
63 ! *** until loop***
64  do while (.true.)
65  call rndc(u)
66  x=1.-u**bi
67  call rndc(u)
68  if ( u .le. x**am)
69  * goto 200
70  enddo
71  200 continue
72  endif
73  end
block data cblkElemag data *AnihiE ! Eposi< 1 TeV, anihilation considered *X0/365.667/, ! radiation length of air in kg/m2 *Ecrit/81.e-3/, ! critical energy of air in GeV *MaxComptonE/1./, ! compton is considered below 1 GeV *MaxPhotoE/1.e-3/, ! above this, PhotoElectric effect neg. *MinPhotoProdE/153.e-3/, ! below 153 MeV, no gp --> hadrons ! scattering const not MeV *Knockon true
Definition: cblkElemag.h:7
subroutine rndc(u)
Definition: rnd.f:91
subroutine kbetar(a, b, x)
Definition: kbetar.f:19