Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / tcoffee / t_coffee_source / random.c
1 /*
2 *       
3 *       Rand.c
4 *       
5 *       -       linear and additive congruential random number generators
6 *               (see R. Sedgewick, Algorithms, Chapter 35)
7 *
8 *       Implementation: R. Fuchs, EMBL Data Library, 1991
9 *       
10 */
11
12
13
14 #define m1      10000
15 #define m       100000000
16
17 /* linear congruential method
18 *       
19 *       linrand() returns an unsigned long random number in the range 0 to r-1
20 */
21
22
23
24 static unsigned long mult(unsigned long p, unsigned long q)
25 {
26         unsigned long p1,p0,q1,q0;
27         
28         p1 = p/m1; p0 = p % m1;
29         q1 = q/m1; q0 = q % m1;
30         return (unsigned long)((((p0*q1 + p1*q0) % m1) * m1 + p0*q0) % m);
31 }
32
33
34 /* additive congruential method
35 *       
36 *       addrand() returns an unsigned long random number in the range 0 to r-1
37 *       The random number generator is initialized by addrandinit()
38 */
39
40 static unsigned long j;
41 static unsigned long a[55];
42
43 unsigned long addrand(unsigned long r)
44 {
45 int x,y;
46 /*        fprintf(stdout,"\n j = %d",j);  */
47         j = (j + 1) % 55;
48 /*        fprintf(stdout,"\n j = %d",j);  */
49         x = (j+23)%55;
50         y = (j+54)%55;
51         a[j] = (a[x] + a[y]) % m;
52 /*      a[j] = (a[(j+23)%55] + a[(j+54)%55]) % m;  */
53 /*        fprintf(stdout,"\n a[j] = %d",a[j]);     */
54         return( ((a[j] / m1) * r) / m1 );
55 }
56
57 void addrandinit(unsigned long s)
58 {
59         a[0] = s;
60         j = 0;
61         do {
62                 ++j;
63                 a[j] = (mult(31,a[j-1]) + 1) % m;
64         } while (j<54);
65 }
66 /******************************COPYRIGHT NOTICE*******************************/
67 /*© Centro de Regulacio Genomica */
68 /*and */
69 /*Cedric Notredame */
70 /*Fri Feb 18 08:27:45 CET 2011 - Revision 596. */
71 /*All rights reserved.*/
72 /*This file is part of T-COFFEE.*/
73 /**/
74 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
75 /*    it under the terms of the GNU General Public License as published by*/
76 /*    the Free Software Foundation; either version 2 of the License, or*/
77 /*    (at your option) any later version.*/
78 /**/
79 /*    T-COFFEE is distributed in the hope that it will be useful,*/
80 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
81 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
82 /*    GNU General Public License for more details.*/
83 /**/
84 /*    You should have received a copy of the GNU General Public License*/
85 /*    along with Foobar; if not, write to the Free Software*/
86 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
87 /*...............................................                                                                                      |*/
88 /*  If you need some more information*/
89 /*  cedric.notredame@europe.com*/
90 /*...............................................                                                                                                                                     |*/
91 /**/
92 /**/
93 /*      */
94 /******************************COPYRIGHT NOTICE*******************************/