genrandInt32 method

int genrandInt32()

Implementation

int genrandInt32() {
  //c//unsigned long y;
  //c//static unsigned long mag01[2]={0x0UL, MATRIX_A};
  int y = 0;
  List<int> mag01 = []; //= new Array(0x0, MATRIX_A);
  mag01.add(0x0);
  mag01.add(matrixA);
  /* mag01[x] = x * MATRIX_A  for x=0,1 */

  if (mti >= N) {
    /* generate N words at one time */
    //c//int kk;
    int kk;

    if (mti == N + 1) {
      initGenrand(5489); /* a default initial seed is used */
    }

    for (kk = 0; kk < N - M; kk++) {
      //c//y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
      //c//mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
      y = unsigned32((mt[kk] & upperMask) | (mt[kk + 1] & lowerMask));
      mt[kk] = unsigned32(mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1]);
    }
    for (; kk < N - 1; kk++) {
      //c//y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
      //c//mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
      y = unsigned32((mt[kk] & upperMask) | (mt[kk + 1] & lowerMask));
      mt[kk] = unsigned32(mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1]);
    }
    //c//y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
    //c//mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
    y = unsigned32((mt[N - 1] & upperMask) | (mt[0] & lowerMask));
    mt[N - 1] = unsigned32(mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1]);
    mti = 0;
  }

  y = mt[mti++];

  /* Tempering */
  //c//y ^= (y >> 11);
  //c//y ^= (y << 7) & 0x9d2c5680;
  //c//y ^= (y << 15) & 0xefc60000;
  //c//y ^= (y >> 18);
  y = unsigned32(y ^ (y >> 11));
  y = unsigned32(y ^ ((y << 7) & 0x9d2c5680));
  y = unsigned32(y ^ ((y << 15) & 0xefc60000));
  y = unsigned32(y ^ (y >> 18));

  return y;
}