Next version of JABA
[jabaws.git] / binaries / src / clustalw / src / tree / RandomGenerator.cpp
1 /**
2  * Author: Mark Larkin
3  * 
4  * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
5  */
6 #ifdef HAVE_CONFIG_H
7     #include "config.h"
8 #endif
9 #include "RandomGenerator.h"
10
11 namespace clustalw
12 {
13
14 /**
15  * The contructor initialises the random algorithm.
16  * @param s 
17  * @return 
18  */
19 RandomGenerator::RandomGenerator(unsigned long s)
20  : m(100000000), m1(10000)
21 {
22     a[0] = s;
23     j = 0;
24     do
25     {
26         ++j;
27         a[j] = (mult(31, a[j - 1]) + 1) % m;
28     }
29     while (j < 54);
30 }
31
32 /**
33  * additive congruential method.
34  * @param r 
35  * @return unsigned long random number in the range 0 to r-1
36  */
37 unsigned long RandomGenerator::addRand(unsigned long r)
38 {
39     int x, y;
40     j = (j + 1) % 55;
41     x = (j + 23) % 55;
42     y = (j + 54) % 55;
43     a[j] = (a[x] + a[y]) % m;
44
45     return (((a[j] / m1) *r) / m1);
46 }
47
48 /**
49  * 
50  * @param p 
51  * @param q 
52  * @return 
53  */
54 unsigned long RandomGenerator::mult(unsigned long p, unsigned long q)
55 {
56     unsigned long p1, p0, q1, q0;
57
58     p1 = p / m1;
59     p0 = p % m1;
60     q1 = q / m1;
61     q0 = q % m1;
62     return ((((p0 *q1 + p1 * q0) % m1) *m1 + p0 * q0) % m);
63 }
64
65 }