Random.dat stuff

From WTFwiki
Revision as of 19:42, 30 November 2009 by Chaosbm (talk | contribs) (Random number generator)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
DWORD g_something;
BYTE g_randSeed1, g_randSeed2, g_randSeed3, g_randSeed4;

void reinit_random_number_generator(unsigned int result)
{
  char seed2 = g_randSeed2;
  if ( !(g_flags & 2) )
  {
    g_randSeed1 = result >> 1;
    g_randSeed3 = result >> 5;
    ++g_something;
    g_randSeed4 = result >> 3;
    seed2 = (BYTE)g_something + 3 * (BYTE)result;
    g_randSeed1 += 3 * (BYTE)g_something;
    g_randSeed4 += g_something >> 1;
    g_randSeed3 += 4 * (BYTE)g_something;
  }
  g_randSeed2 = seed2;
}

int random(int max)
{
  static char table[256];
  static int initialized = 0;

  if ( !initialized )
  {
    int file = open("random.dat", 512);
    assert(file != -1, "ERROR random.dat not found\n");
    read(file, table, 256);
    close(file);
    initialized = 1;
  }

  int result;
  if ( max <= 255 )
  {
    result = max * (g_randomTable[g_randSeed1] ^ g_randomTable[g_randSeed2] ^ (unsigned int)g_randomTable[g_randSeed3]) >> 8;
  }
  else if ( max <= 65535 )
  {
    result = max * (((g_randomTable[g_randSeed3] ^ g_randomTable[g_randSeed1] ^ g_randomTable[g_randSeed4]) << 8) | g_randomTable[g_randSeed2] ^ g_randomTable[g_randSeed1] ^ (unsigned int)g_randomTable[g_randSeed3]) >> 16;
  }
  else
  {
    result = g_randomTable[g_randSeed4] ^ ((max >> 8) * (g_randomTable[g_randSeed3] ^ g_randomTable[g_randSeed1] ^ g_randomTable[g_randSeed2] | ((unsigned int)g_randomTable[g_randSeed4] << 8)) >> 16 << 8);
  }
  g_randSeed1 -= 2;
  g_randSeed2 += 1;
  g_randSeed3 += 3;
  g_randSeed4 += 7;
  return result;
}