/[MITgcm]/MITgcm/model/src/port_rand.F
ViewVC logotype

Contents of /MITgcm/model/src/port_rand.F

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download)
Tue Mar 1 16:51:27 2005 UTC (19 years, 3 months ago) by jmc
Branch: MAIN
CVS Tags: checkpoint57m_post, checkpoint57g_pre, checkpoint57s_post, checkpoint58b_post, checkpoint57g_post, checkpoint57y_post, checkpoint57r_post, checkpoint57i_post, checkpoint59, checkpoint58, checkpoint58f_post, checkpoint57n_post, checkpoint58d_post, checkpoint58a_post, checkpoint57z_post, checkpoint58y_post, checkpoint58t_post, checkpoint58m_post, checkpoint57l_post, checkpoint57t_post, checkpoint57v_post, checkpoint57f_post, checkpoint60, checkpoint61, checkpoint57h_pre, checkpoint58w_post, checkpoint57h_post, checkpoint57y_pre, checkpoint58o_post, checkpoint58p_post, checkpoint58q_post, checkpoint58e_post, mitgcm_mapl_00, checkpoint58r_post, checkpoint58n_post, checkpoint59q, checkpoint59p, checkpoint59r, checkpoint59e, checkpoint59d, checkpoint59g, checkpoint59f, checkpoint59a, checkpoint59c, checkpoint59b, checkpoint59m, checkpoint59l, checkpoint59o, checkpoint59n, checkpoint59i, checkpoint59h, checkpoint59k, checkpoint57p_post, checkpint57u_post, checkpoint57q_post, checkpoint58k_post, checkpoint58v_post, checkpoint58l_post, checkpoint57h_done, checkpoint57j_post, checkpoint57f_pre, checkpoint61f, checkpoint58g_post, checkpoint58x_post, checkpoint61n, checkpoint59j, checkpoint58h_post, checkpoint58j_post, checkpoint57o_post, checkpoint61q, checkpoint57k_post, checkpoint57w_post, checkpoint61e, checkpoint58i_post, checkpoint57x_post, checkpoint58c_post, checkpoint58u_post, checkpoint58s_post, checkpoint61g, checkpoint61d, checkpoint61b, checkpoint61c, checkpoint61a, checkpoint61o, checkpoint61l, checkpoint61m, checkpoint61j, checkpoint61k, checkpoint61h, checkpoint61i, checkpoint61v, checkpoint61w, checkpoint61t, checkpoint61u, checkpoint61r, checkpoint61s, checkpoint61p, checkpoint61z, checkpoint61x, checkpoint61y
Changes since 1.5: +56 -31 lines
Allow to set the seed ;
turn back to the original port_rand function if called with -1.

1 C $Header: /u/gcmpack/MITgcm/model/src/port_rand.F,v 1.5 2004/09/07 15:16:58 edhill Exp $
2 C $Name: $
3
4 C Portable random number generator
5
6 #undef _USE_INTEGERS
7
8 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
9 CBOP
10 C !ROUTINE: port_rand
11 C !INTERFACE:
12 real*8 function port_rand(seed)
13
14 C !DESCRIPTION:
15 C Portable random number generator
16 C seed >=0 :: initialise using this seed ; and return 0
17 C seed < 0 :: if first call then initialise using the default seed (=mseed)
18 C and always return a random number
19
20 C !USES:
21 implicit none
22 C !INPUT PARAMETERS:
23 #ifdef _USE_INTEGERS
24 integer seed
25 #else
26 real*8 seed
27 #endif
28 CEOP
29
30 C !LOCAL VARIABLES:
31 integer nff,idum
32 parameter(nff=55)
33 parameter(idum=-2)
34 real*8 fac
35 #ifdef _USE_INTEGERS
36 integer mbig,mseed,mZ
37 parameter (mbig=1000000000,mz=0,fac=1.d0/mbig)
38 integer mj,mk,ma(nff)
39 data mseed/161803398/
40 #else
41 real*8 mbig,mseed,mz
42 parameter (mbig=4000000.,mz=0.,fac=1.d0/mbig)
43 real*8 mj,mk,ma(nff)
44 data mseed/1618033./
45 #endif
46 logical firstCall
47 integer i,ii,inext,inextp,k
48 data firstCall /.true./
49 save firstCall,inext,inextp,ma
50
51 C- Initialise the random number generator
52 if(firstCall .OR. seed.GE.mz)then
53 if (seed.GE.mz) mseed = seed
54 firstCall=.false.
55 mj=mseed-iabs(idum)
56 mj=mod(mj,mbig)
57 ma(nff)=mj
58 mk=1
59 do i=1,nff-1
60 ii=mod(21*i,nff)
61 ma(ii)=mk
62 mk=mj-mk
63 if(mk.lt.mz)mk=mk+mbig
64 mj=ma(ii)
65 enddo
66 do k=1,4
67 do i=1,nff
68 ma(i)=ma(i)-ma(1+mod(i+30,nff))
69 if(ma(i).lt.mz)ma(i)=ma(i)+mbig
70 enddo
71 enddo
72 inext=0
73 inextp=31
74 endif
75
76 C- Compute a random number (only if seed < 0)
77 if(seed.GE.mz)then
78 port_rand=0.d0
79 else
80 inext=mod(inext,nff)+1
81 inextp=mod(inextp,nff)+1
82 mj=ma(inext)-ma(inextp)
83 if(mj.lt.mz)mj=mj+mbig
84 ma(inext)=mj
85 port_rand=mj*fac
86 endif
87
88 return
89 end
90
91 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
92
93 subroutine port_ranarr(n,arr)
94 implicit none
95 integer n,i
96 real arr(n)
97 real*8 port_rand
98 #ifdef _USE_INTEGERS
99 integer seed
100 seed=-1
101 #else
102 real*8 seed
103 seed=-1.d0
104 #endif
105 c seed=1618033.0d0
106 do i=1,n
107 arr(i)=port_rand(seed)
108 enddo
109
110 return
111 end
112
113 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
114 CBOP
115 C !ROUTINE: port_rand_norm
116 C !INTERFACE:
117 real*8 function port_rand_norm()
118
119 C !DESCRIPTION:
120 C This function generates a normally distributed random number with
121 C the so called polar algorithm. The algorithm actually generates 2
122 C numbers, but only 1 is returned for maximum compatibility with old
123 C code. The most obvious way to improve this function would be to
124 C make sure that the second number is not wasted.
125
126 C Changed: 2004.09.06 antti.westerlund@fimr.fi
127
128 C !USES:
129 implicit none
130 CEOP
131
132 C !LOCAL VARIABLES:
133 real*8 port_rand
134 real*8 x1, x2, xs, t
135 integer i
136
137 #ifdef _USE_INTEGERS
138 integer seed
139 seed=-1
140 #else
141 real*8 seed
142 seed=-1.d0
143 #endif
144 c seed=1618033.0d0
145
146 C first generate 2 equally distributed random numbers (-1,1)
147 DO WHILE (1 .eq. 1)
148 x1=2.0*port_rand(seed)-1.0
149 x2=2.0*port_rand(seed)-1.0
150 xs=x1**2+x2**2
151 if(xs .lt. 1.0 .and. xs .ne. 0.0) then
152 goto 100
153 end if
154 END DO
155 100 continue
156
157 t = sqrt(-2.0*log(xs)/xs)
158 port_rand_norm = t*x1
159 C
160 C also t*x2 would be a gaussian random number and could be returned
161
162 return
163 end

  ViewVC Help
Powered by ViewVC 1.1.22