Skip to content

Randomness

widberg edited this page Jan 4, 2024 · 8 revisions

rand

microsoft.com rand
cppreference.com rand
Understanding the algorithm of Visual C++'s rand() function

Observed being called by WeatherManager_G.

#define RAND_MAX 0x7fff

int rand() {
  _ptiddata ptd = _getptd();
  unsigned int r = 214013 * ptd->_holdrand + 2531011;
  ptd->_holdrand = r;
  return (r >> 16) & RAND_MAX;
}

Random::ran1()

Lehmer random number generator

Gets called way more often than rand.

Constants and Static State

static const int    IA    = 16807;
static const int    IM    = 0x7FFFFFFF;
static const int    IQ    = 127773;
static const int    NTAB  = 32;
static const int    NDIV  = (1+(IM-1)/NTAB); // 1+(0x7FFFFFFF-1)/32 = 0x4000000
static const double AM    = (1.0f/IM); // 1.0f/0x7FFFFFFF = 4.6566129e-10
static const double EPS   = 1.2e-7f;
static const double RNMX  = (1.0f-EPS); // 0.99999988

static int idum;
static int m_iY;
static int m_iV[32];

InitRandomSeed Command

char CallbackInitRandomSeed() {
  if (GlobalCommandManager->argc < 1 || !GlobalCommandManager->valueHasFloat[1])
    return 0;

  idum = (int)GlobalCommandManager->valueAsFloat[1];
  if (idum >= 0)
    idum = -idum;

  Random_Z::ran1();

  return 1;
}

Random::ran1()

double Random_Z::ran1() {
  if (idum <= 0 || !m_iY) {
    idum = -idum;
    if (idum < 1)
      idum = 1;

    for (int j = NTAB + 7; j >= 0; j--) {
      int k = idum / IQ;
      idum = IA * idum - IM * k;
      if (idum < 0)
        idum += IM;
      if (j < NTAB)
        m_iV[j] = idum;
    }

    m_iY = m_iV[0];
  }

  int k = idum / IQ;
  idum = IA * idum - IM * k;
  if (idum < 0)
    idum += IM;

  int j = m_iY / NDIV;
  m_iY = m_iV[j];
  m_iV[j] = idum;
  
  double temp = AM * m_iY;
  if (temp > RNMX)
    return RNMX;
  return temp;
}

Home
FAQ

For FMTK Users and Mod Developers

Read the Docs

For FMTK Developers

Asobo BigFile Format Specification
Asobo Classes
      Animation_Z
      Binary_Z
      Bitmap_Z
      Camera_Z
      CollisionVol_Z
      Fonts_Z
      GameObj_Z
      GenWorld_Z
      GwRoad_Z
      Keyframer*_Z
      Light_Z
      LightData_Z
      Lod_Z
      LodData_Z
      Material_Z
      MaterialAnim_Z
      MaterialObj_Z
      Mesh_Z
      MeshData_Z
      Node_Z
      Omni_Z
      Particles_Z
      ParticlesData_Z
      RotShape_Z
      RotShapeData_Z
      Rtc_Z
      Skel_Z
      Skin_Z
      Sound_Z
      Spline_Z
      SplineGraph_Z
      Surface_Z
      SurfaceDatas_Z
      UserDefine_Z
      Warp_Z
      World_Z
      WorldRef_Z
Asobo File Format Idioms
Asobo CRC32
Asobo LZ Compression
Asobo Arithmetic Coding Compression
Asobo Save Game File Format Specification
Asobo Audio Formats
TotemTech/ToonTech/Zouna/ACE/BSSTech/Opal Timeline
Zouna Modding Resources
Miscellaneous

Clone this wiki locally