# Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 8.32 KB  |  hits: 10  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #include <stdio.h>
2. #include <string.h>
3. #include <math.h>
4. // Initiallization routine for random number
5. void   RandomInitialise(int,int);
6. // Call to generate a random number between 0  and 10
7. double RandomUniform(void);
8. // Call to generate a Gaussian random number
9. double RandomGaussian(double,double);
10. // Call to generate a random integer between the two arguments
11. int    RandomInt(int,int);
12. // call to generate a random double between the two arguments
13. double RandomDouble(double,double);
14. FILE *ofp;
15. int main()
16. {
17.         int i=0; int j=0; double k=0; double m=4; int N = 300; double c=0; int steps=10;  int a =0; int b =0; int x =0; int y =0;
18.         double Water_x[1000] = {0.0}; double Water_y[1000]={0.0}; double D = 0.0; double temp1_x =0.0; double temp1_y =0.0;
19.         double Radius= 1; double Inner_Radius = 0.01; double sigma = 3.165; double epsilon = 0.6502;
20.         double temp2_x = 0.0; double temp2_y = 0.0; double d_x =0.0; double d_y =0.0;
21.         double E =0; double Random_Energy = 0.0; double b0 = 1.0; int g=0; int h=0;
22.
23.         ofp = fopen("tweak.txt","w");
24.         RandomInitialise(1802,9373);
25.         ////Initialize a Lattice for all the molecules to begin from....
26.
27.         for(i=0; i<N; i++)
28.         {
29.                 Water_x[i] = c;
30.                 Water_y[i] = m--;
31.                 if(m==0)
32.                 {
33.                         m=4;
34.                         c++;
35.                 }
36.         }
37.
38.
40.         for(k=0; k<N*N*N;k++)
41.         {
42.                 //particle picked at random
43.                 a = RandomInt(0,N);
44.                 b = RandomInt(0,N);
45.                 //Original Value stored in temporary variable.
46.                 temp1_x = Water_x[a];
47.                 temp1_y = Water_y[b];
48.                 //position changed at random
49.                 Water_x[a] = b0*RandomDouble(-2,2);
50.                 Water_y[b] = b0*RandomDouble(-2,2);
51.                 //      fprintf(ofp, "New position %f, %f\n", Water_x[a], Water_y[b]);
52.
53.                 //Distance calculation for neighboring particles.
54.
55.                 for(i=0; i<N; i++)
56.                 {
57.                         d_x = pow(((Water_x[a] + Radius) - Water_x[i]),2);
58.                         d_y = pow(( (Water_y[b] + Radius) - Water_y[i]),2);
59.                         D = sqrt(d_x + d_y);
60.
62.                         {
63.                                 Water_x[a] = temp1_x;
64.                                 Water_y[b] = temp1_y;
65.                                 break;
66.                         }
68.                         {
69.                                 E = 4*epsilon*(pow((sigma/D),12) - pow((sigma/D),6));
70.                                 Random_Energy = RandomInt(0,500000000000);
71.                                 if(Random_Energy<E)
72.                                 {
73.                                         Water_x[a] = temp1_x;
74.                                         Water_y[b] = temp1_y;
75.                                 }
76.                         }
77.                 }
78.         }
79.         k=0;
80.         for(i=0; i<N; i++)
81.         {
82.                 fprintf(ofp,"%f\n", Water_x[i]);
83.                 //fprintf(ofp,"\n");
84.                 //fprintf(ofp,"%f\n", Water_y[i]);
85.         }
86.         fprintf(ofp,"\n");
87.         for(i=0; i<N; i++)
88.         {
89.                 fprintf(ofp,"%f\n", Water_y[i]);
90.         }
91.
92.
93.
94. }
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105. #define FALSE 0
106. #define TRUE 1
107.
108. /*
109.
110. This Random Number Generator is based on the algorithm in a FORTRAN
112.
113. University; ref.: see original comments below.
114.
115. At the fhw (Fachhochschule Wiesbaden, W.Germany), Dept. of Computer
116. Science, we have written sources in further languages (C, Modula-2
117. Turbo-Pascal(3.0, 5.0), Basic and Ada) to get exactly the same test
118. results compared with the original FORTRAN version.
119. April 1989
120. Karl-L. Noell <NOELL@DWIFH1.BITNET>
121. and  Helmut  Weber <WEBER@DWIFH1.BITNET>
122.
123. This random number generator originally appeared in "Toward a Universal
124. Random Number Generator" by George Marsaglia and Arif Zaman.
125. Florida State University Report: FSU-SCRI-87-50 (1987)
126. It was later modified by F. James and published in "A Review of Pseudo-
127. random Number Generators"
128.
129. THIS IS THE BEST KNOWN RANDOM NUMBER GENERATOR AVAILABLE.
130.
131. (However, a newly discovered technique can yield
132. a period of 64^600. But that is still in the development stage.)
133. It passes ALL of the tests for random number generators and has a period
134. of 2^144, is completely portable (gives bit identical results on all
135. machines with at least 24-bit mantissas in the floating point
136. representation).
137.
138. The algorithm is a combination of a Fibonacci sequence (with lags of 97
139. and 33, and operation "subtraction plus one, modulo one") and an
140. "arithmetic sequence" (using subtraction).
141.
142.
143. Use IJ = 1802 & KL = 9373 to test the random number generator. The
144. subroutine RANMAR should be used to generate 20000 random numbers.
145. Then display the next six random numbers generated multiplied by 4096*4096
146. If the random number generator is working properly, the random numbers
147.
148. should be:
149.
150. 6533892.0  14220222.0  7275067.0
151.
152. 6172232.0  8354498.0   64633180.0
153.
154. */
155.
156.
157. /* Globals */
158.
159. double u[97],c,cd,cm;
160.
161. int i97,j97;
162.
163. int test = FALSE;
164.
165.
166. /*
167.
168. This is the initialization routine for the random number generator.
169. NOTE: The seed variables can have values between:    0 <= IJ <= 31328
170. 0 <= KL <= 30081
171.
172. The random number sequences created by these two seeds are of sufficient
173. length to complete an entire calculation with. For example, if sveral
174. different groups are working on different parts of the same calculation,
175. each group could be assigned its own IJ seed. This would leave each group
176. with 30000 choices for the second seed. That is to say, this random
177. number generator can create 900 million different subsequences -- with
178. each subsequence having a length of approximately 64^30.
179.
180. */
181.
182. void RandomInitialise(int ij,int kl)
183.
184. {
185.
186.         double s,t;
187.
188.         int ii,i,j,k,l,jj,m;
189.
190.
191.         /*
192.
193.         Handle the seed range errors
194.
195.         First random number seed must be between 0 and 31328
196.
197.         Second seed must have a value between 0 and 30081
198.
199.         */
200.
201.         if (ij < 0 || ij > 31328 || kl < 0 || kl > 30081) {
202.
203.                 ij = 1802;
204.
205.                 kl = 9373;
206.
207.         }
208.
209.
210.         i = (ij / 177) % 177 + 2;
211.
212.         j = (ij % 177)       + 2;
213.
214.         k = (kl / 169) % 178 + 1;
215.
216.         l = (kl % 169);
217.
218.
219.         for (ii=0; ii<97; ii++) {
220.
221.                 s = 0.0;
222.
223.                 t = 0.5;
224.
225.                 for (jj=0; jj<24; jj++) {
226.
227.                         m = (((i * j) % 179) * k) % 179;
228.
229.                         i = j;
230.
231.                         j = k;
232.
233.                         k = m;
234.
235.                         l = (53 * l + 1) % 169;
236.
237.                         if (((l * m % 64)) >= 32)
238.
239.                                 s += t;
240.
241.                         t *= 0.5;
242.
243.                 }
244.
245.                 u[ii] = s;
246.
247.         }
248.
249.
250.         c    = 362436.0 / 16777216.0;
251.
252.         cd   = 7654321.0 / 16777216.0;
253.
254.         cm   = 16777213.0 / 16777216.0;
255.
256.         i97  = 97;
257.
258.         j97  = 33;
259.
260.         test = TRUE;
261.
262. }
263.
264.
265. /*
266.
267. This is the random number generator proposed by George Marsaglia in
268.
269. Florida State University Report: FSU-SCRI-87-50
270.
271. */
272.
273. double RandomUniform(void)
274.
275. {
276.
277.         double uni;
278.
279.
280.         /* Make sure the initialisation routine has been called */
281.
282.         if (!test)
283.
284.                 RandomInitialise(1802,9373);
285.
286.
287.         uni = u[i97-1] - u[j97-1];
288.
289.         if (uni <= 0.0)
290.
291.                 uni++;
292.
293.         u[i97-1] = uni;
294.
295.         i97--;
296.
297.         if (i97 == 0)
298.
299.                 i97 = 97;
300.
301.         j97--;
302.
303.         if (j97 == 0)
304.
305.                 j97 = 97;
306.
307.         c -= cd;
308.
309.         if (c < 0.0)
310.
311.                 c += cm;
312.
313.         uni -= c;
314.
315.         if (uni < 0.0)
316.
317.                 uni++;
318.
319.
320.         return(uni);
321.
322. }
323.
324.
325. /*
326.
327. ALGORITHM 712, COLLECTED ALGORITHMS FROM ACM.
328.
329. THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
330.
331. VOL. 18, NO. 4, DECEMBER, 1992, PP. 434-435.
332.
333. The function returns a normally distributed pseudo-random number
334.
335. with a given mean and standard devaiation.  Calls are made to a
336.
337. function subprogram which must return independent random
338.
339. numbers uniform in the interval (0,1).
340.
341. The algorithm uses the ratio of uniforms method of A.J. Kinderman
342.
343. and J.F. Monahan augmented with quadratic bounding curves.
344.
345. */
346.
347. double RandomGaussian(double mean,double stddev)
348.
349. {
350.
351.         double  q,u,v,x,y;
352.
353.
354.         /*
355.
356.         Generate P = (u,v) uniform in rect. enclosing acceptance region
357.
358.         Make sure that any random numbers <= 0 are rejected, since
359.
360.         gaussian() requires uniforms > 0, but RandomUniform() delivers >= 0.
361.
362.         */
363.
364.         do {
365.
366.                 u = RandomUniform();
367.
368.                 v = RandomUniform();
369.
370.                 if (u <= 0.0 || v <= 0.0) {
371.
372.                         u = 1.0;
373.
374.                         v = 1.0;
375.
376.                 }
377.
378.                 v = 1.7156 * (v - 0.5);
379.
380.
381.                 /*  Evaluate the quadratic form */
382.
383.                 x = u - 0.449871;
384.
385.                 y = fabs(v) + 0.386595;
386.
387.                 q = x * x + y * (0.19600 * y - 0.25472 * x);
388.
389.
390.                 /* Accept P if inside inner ellipse */
391.
392.                 if (q < 0.27597)
393.
394.                         break;
395.
396.
397.                 /*  Reject P if outside outer ellipse, or outside acceptance region */
398.
399.         } while ((q > 0.27846) || (v * v > -4.0 * log(u) * u * u));
400.
401.
402.         /*  Return ratio of P's coordinates as the normal deviate */
403.
404.         return (mean + stddev * v / u);
405.
406. }
407.
408.
409. /*
410.
411. Return random integer within a range, lower -> upper INCLUSIVE
412.
413. */
414.
415. int RandomInt(int lower,int upper)
416. {
417.         return((int)(RandomUniform() * (upper - lower + 1)) + lower);
418. }
419.
420.
421. /*
422.
423. Return random float within a range, lower -> upper
424.
425. */
426. double RandomDouble(double lower,double upper){return((upper - lower) * RandomUniform() + lower);}