File: rhrand.h

package info (click to toggle)
neartree 3.1.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,068 kB
  • ctags: 610
  • sloc: cpp: 7,826; ansic: 5,612; makefile: 445
file content (160 lines) | stat: -rw-r--r-- 5,114 bytes parent folder | download | duplicates (5)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *  rhrand.h
 *  RHrand
 *  
 *  Based on:
 *  Random Number generator by Rob Harrison, derived from 
 *  "one in J.M.Hammersley and D.C. Handscomb, "Monte Carlo 
 *  Methods," Methuen & Co., London and Wiley & Sons, New 
 *  York, 1964, p47".  See also, D. E. Knuth "The Art of 
 *  Computer Programming", Volume 2, "Seminumerical
 *  Alogorithms, Third Edition, Addison-Wesley, Reading MA,
 *  1997.
 *
 *  This adaptation by H. J. Bernstein
 *  Copyright 2009 Rob Harrison, Larry Andrews and Herbert J. Bernstein
 *
 */

/**********************************************************************
 *                                                                    *
 * YOU MAY REDISTRIBUTE THE rhrand API UNDER THE TERMS OF THE LGPL    *
 *                                                                    *
 **********************************************************************/

/************************* LGPL NOTICES *******************************
 *                                                                    *
 * This library is free software; you can redistribute it and/or      *
 * modify it under the terms of the GNU Lesser General Public         *
 * License as published by the Free Software Foundation; either       *
 * version 2.1 of the License, or (at your option) any later version. *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                    *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License along with this library; if not, write to the Free         *
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,    *
 * MA  02110-1301  USA                                                *
 *                                                                    *
 **********************************************************************/

#ifndef RHRAND_H_INCLUDED
#define RHRAND_H_INCLUDED

#ifdef __cplusplus

#include <cmath>

class RHrand {
    
private:
    double randomNumberBuffer[56];
    int indx;
    int jndx;
    int kndx;
    double dTemp;
    
public:
    static const int RHRAND_MAX = 32767;
    
    RHrand( void ) {                 /* default constructor */
        srandom(0);
    };
    
    
    RHrand( const int iseed ) {            /* constructor with seed */
        srandom(iseed);
    };
    
    ~RHrand(void ) {                /* nothing to do for destructor */ 
    };
    
    void srandom ( const int iseed ) {
        jndx = iseed;
        if (jndx < 0) jndx = -jndx;
        for( indx=0; indx<(int)(sizeof(randomNumberBuffer)/sizeof(randomNumberBuffer[0])); ++indx )
        {
            jndx = (jndx*2349 + 14867)%32767;
            randomNumberBuffer[indx] = fabs((double)(jndx/32767.0));
        }
        indx = 55;
        kndx = 54;
        jndx = 31;
        return;
    };
    
    double urand( void ) {
        indx = indx%55 + 1;
        jndx = jndx%55 + 1;
        kndx = kndx%55 + 1;
        randomNumberBuffer[indx-1] = modf( randomNumberBuffer[jndx-1]+randomNumberBuffer[kndx-1], &dTemp );
        return  randomNumberBuffer[indx-1];
    };
    
    int random( void ) {
        return (int)(urand()*((double)RHRAND_MAX));
    };
    
};

#ifndef RHRAND_NOCCODE
extern "C" {
#endif
    
#endif

#ifndef RHRAND_NOCCODE

#include <math.h>
    
    typedef struct CRHrand_ {
        double buffer[56];
        int indx;
        int jndx;
        int kndx;
        double dTemp;
    } CRHrand;
    
    typedef CRHrand * CRHrandHandle;
    
#define CRHRAND_MAX 32767

#define CRHrandSrandom(randhandle,iseed) { \
        (randhandle)->jndx = iseed; \
        if ((randhandle)->jndx < 0) (randhandle)->jndx = -(randhandle)->jndx; \
        for((randhandle)->indx=0; (randhandle)->indx<(sizeof((randhandle)->buffer)/sizeof((randhandle)->buffer[0])); ++(randhandle)->indx ) \
        { \
            (randhandle)->jndx = ((randhandle)->jndx*2349 + 14867)%32767; \
            ((randhandle)->buffer)[(randhandle)->indx] = fabs((double)(((randhandle)->jndx)/32767.0)); \
        } \
        (randhandle)->indx = 55; \
        (randhandle)->kndx = 54; \
        (randhandle)->jndx = 31; \
    }
#define CRHrandUrand(randhandle) ( \
        (randhandle)->indx = (randhandle)->indx%55 + 1, \
        (randhandle)->jndx = (randhandle)->jndx%55 + 1, \
        (randhandle)->kndx = (randhandle)->kndx%55 + 1, \
        ((randhandle)->buffer)[(randhandle)->indx-1] \
        = modf( ((randhandle)->buffer)[(randhandle)->jndx-1]+ \
               ((randhandle)->buffer)[(randhandle)->kndx-1], &(randhandle)->dTemp ) , \
        ((randhandle)->buffer)[(randhandle)->indx-1]  ) 

#define CRHrandRandom(randhandle) ((int)(CRHrandUrand(randhandle)*(double)CRHRAND_MAX))
    
    
    
#ifdef __cplusplus
    
}

#endif
#endif


#endif