-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbnldev.f
48 lines (48 loc) · 1.07 KB
/
bnldev.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
FUNCTION BNLDEV(PP,N,IDUM)
PARAMETER (PI=3.141592654)
DATA NOLD /-1/, POLD /-1./
IF(PP.LE.0.5)THEN
P=PP
ELSE
P=1.-PP
ENDIF
AM=N*P
IF (N.LT.25)THEN
BNLDEV=0.
DO 11 J=1,N
IF(RAN1(IDUM).LT.P)BNLDEV=BNLDEV+1.
11 CONTINUE
ELSE IF (AM.LT.1.) THEN
G=EXP(-AM)
T=1.
DO 12 J=0,N
T=T*RAN1(IDUM)
IF (T.LT.G) GO TO 1
12 CONTINUE
J=N
1 BNLDEV=J
ELSE
IF (N.NE.NOLD) THEN
EN=N
OLDG=GAMMLN(EN+1.)
NOLD=N
ENDIF
IF (P.NE.POLD) THEN
PC=1.-P
PLOG=LOG(P)
PCLOG=LOG(PC)
POLD=P
ENDIF
SQ=SQRT(2.*AM*PC)
2 Y=TAN(PI*RAN1(IDUM))
EM=SQ*Y+AM
IF (EM.LT.0..OR.EM.GE.EN+1.) GO TO 2
EM=INT(EM)
T=1.2*SQ*(1.+Y**2)*EXP(OLDG-GAMMLN(EM+1.)
* -GAMMLN(EN-EM+1.)+EM*PLOG+(EN-EM)*PCLOG)
IF (RAN1(IDUM).GT.T) GO TO 2
BNLDEV=EM
ENDIF
IF (P.NE.PP) BNLDEV=N-BNLDEV
RETURN
END