File: RandomGenerator.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,868 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 15
file content (65 lines) | stat: -rw-r--r-- 1,113 bytes parent folder | download | duplicates (6)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include "RandomGenerator.h"

namespace clustalw
{

/**
 * The constructor initialises the random algorithm.
 * @param s 
 * @return 
 */
RandomGenerator::RandomGenerator(unsigned long s)
 : m(100000000), m1(10000)
{
    a[0] = s;
    j = 0;
    do
    {
        ++j;
        a[j] = (mult(31, a[j - 1]) + 1) % m;
    }
    while (j < 54);
}

/**
 * additive congruential method.
 * @param r 
 * @return unsigned long random number in the range 0 to r-1
 */
unsigned long RandomGenerator::addRand(unsigned long r)
{
    int x, y;
    j = (j + 1) % 55;
    x = (j + 23) % 55;
    y = (j + 54) % 55;
    a[j] = (a[x] + a[y]) % m;

    return (((a[j] / m1) *r) / m1);
}

/**
 * 
 * @param p 
 * @param q 
 * @return 
 */
unsigned long RandomGenerator::mult(unsigned long p, unsigned long q)
{
    unsigned long p1, p0, q1, q0;

    p1 = p / m1;
    p0 = p % m1;
    q1 = q / m1;
    q0 = q % m1;
    return ((((p0 *q1 + p1 * q0) % m1) *m1 + p0 * q0) % m);
}

}