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