COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/mingw32_rand.cc @ 2035:e92071fadd3f

Last change on this file since 2035:e92071fadd3f was 2035:e92071fadd3f, checked in by Balazs Dezso, 18 years ago

More mingw compatibility

Implementation of the drand48 functions

File size: 2.1 KB
Line 
1#ifdef WIN32
2
3#include <lemon/bits/mingw32_rand.h>
4
5
6static unsigned short _mingw_rand_state[10];
7
8void _mingw_rand_next_state(unsigned short xsubi[3]) {
9  _mingw_rand_state[8] = _mingw_rand_state[9] = 0;
10  _mingw_rand_state[7] =  _mingw_rand_state[6];
11  for (int i = 0; i < 3; ++i) {
12    unsigned long val = 0;
13    for (int j = 0; i + j < 3; ++j) {
14      val += (unsigned long)_mingw_rand_state[7 + i + j]
15        + (unsigned long)xsubi[i] * (unsigned long)_mingw_rand_state[3 + j];
16      _mingw_rand_state[7 + i + j] = val;
17      val >>= 16;
18    }
19  }
20  xsubi[0] = _mingw_rand_state[6];
21  xsubi[1] = _mingw_rand_state[7];
22  xsubi[2] = _mingw_rand_state[8];
23}
24
25long int lrand48(void) {
26  return nrand48(_mingw_rand_state);
27}
28
29long int nrand48(unsigned short xsubi[3]) {
30  _mingw_rand_next_state(xsubi);
31  return ((long)(xsubi[2] & ( (1 << 15) - 1) ) << 16) | (long)xsubi[1];
32}
33
34double drand48(void) {
35  return erand48(_mingw_rand_state);
36}
37
38double erand48(unsigned short xsubi[3]) {
39  return (double)nrand48(xsubi) / (1 << 31);
40}
41
42
43long int mrand48(void) {
44  return jrand48(_mingw_rand_state);
45}
46
47long int jrand48(unsigned short xsubi[3]) {
48  _mingw_rand_next_state(xsubi);
49  return ((long)xsubi[2] << 16) | (long)xsubi[1];
50}
51
52void srand48(long int seedval) {
53  _mingw_rand_state[0] = 0x330E;
54  _mingw_rand_state[1] = seedval & ( (1 << 16) - 1);
55  _mingw_rand_state[2] = seedval >> 16;
56  _mingw_rand_state[3] = 0xE66D;
57  _mingw_rand_state[4] = 0xDEEC;
58  _mingw_rand_state[5] = 0x0005;
59  _mingw_rand_state[6] = 0x000B;
60}
61
62unsigned short *seed48(unsigned short seed16v[3]) {
63  _mingw_rand_state[7] = _mingw_rand_state[0];
64  _mingw_rand_state[8] = _mingw_rand_state[1];
65  _mingw_rand_state[9] = _mingw_rand_state[2];
66  _mingw_rand_state[0] = seed16v[0];
67  _mingw_rand_state[1] = seed16v[1];
68  _mingw_rand_state[2] = seed16v[2];
69  return _mingw_rand_state + 7;
70}
71
72void lcong48(unsigned short param[7]) {
73  _mingw_rand_state[0] = param[0];
74  _mingw_rand_state[1] = param[1];
75  _mingw_rand_state[2] = param[2];
76  _mingw_rand_state[3] = param[3];
77  _mingw_rand_state[4] = param[4];
78  _mingw_rand_state[5] = param[5];
79  _mingw_rand_state[6] = param[6];
80}
81
82
83#endif
Note: See TracBrowser for help on using the repository browser.