source: tags/ms_r16q3/ARBDB/admath.cxx

Last change on this file was 15176, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.8 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : admath.cxx                                        //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include <cmath>
12#include <ctime>
13
14#include "gb_local.h"
15
16double GB_log_fak(int n) {
17    // returns log(n!)
18    static int     max_n = 0;
19    static double *res   = 0;
20
21    if (n<=1) return 0.0;       // log 1 = 0
22
23    if (n >= max_n) {
24        double sum = 0;
25
26        max_n = n + 100;
27        freeset(res, ARB_calloc<double>(max_n));
28
29        for (int i=1; i<max_n; i++) {
30            sum += log((double)i);
31            res[i] = sum;
32        }
33    }
34    return res[n];
35}
36
37// ----------------------------------
38//      random number generation
39
40static int      randomSeeded = 0;
41static unsigned usedSeed     = 0;
42
43void GB_random_seed(unsigned seed) {
44    /*! normally you should not use GB_random_seed.
45     * Use it only to reproduce some result (e.g. in unittests)
46     */
47    usedSeed     = seed;
48    srand(seed);
49    randomSeeded = 1;
50}
51
52int GB_random(int range) {
53    // produces a random number in range [0 .. range-1]
54    if (!randomSeeded) GB_random_seed(time(0));
55
56#if defined(DEBUG)
57    if (range>RAND_MAX) {
58        printf("Warning: range to big for random granularity (%i > %i)\n", range, RAND_MAX);
59    }
60#endif // DEBUG
61
62    return (int)(rand()*((double)range) / (RAND_MAX+1.0));
63}
Note: See TracBrowser for help on using the repository browser.