/* Ofront+ 1.0 -sC -48 */
#include "SYSTEM.oh"


static INTEGER ooc2RandomNumbers_z;


export void ooc2RandomNumbers_GetSeed (INTEGER *seed);
static void ooc2RandomNumbers_NextRND (void);
export void ooc2RandomNumbers_PutSeed (INTEGER seed);
export INTEGER ooc2RandomNumbers_RND (INTEGER range);
export SHORTREAL ooc2RandomNumbers_Random (void);


/*============================================================================*/

void ooc2RandomNumbers_GetSeed (INTEGER *seed)
{
  *seed = ooc2RandomNumbers_z;
}

/*----------------------------------------------------------------------------*/
void ooc2RandomNumbers_PutSeed (INTEGER seed)
{
  seed = __MOD(seed, 2147483647);
  if (seed == 0) {
    ooc2RandomNumbers_z = 1;
  } else {
    ooc2RandomNumbers_z = seed;
  }
}

/*----------------------------------------------------------------------------*/
static void ooc2RandomNumbers_NextRND (void)
{
  INTEGER lo, hi, test;
  hi = __DIV(ooc2RandomNumbers_z, 127773);
  lo = __MOD(ooc2RandomNumbers_z, 127773);
  test = 16807 * lo - 2836 * hi;
  if (test > 0) {
    ooc2RandomNumbers_z = test;
  } else {
    ooc2RandomNumbers_z = test + 2147483647;
  }
}

INTEGER ooc2RandomNumbers_RND (INTEGER range)
{
  ooc2RandomNumbers_NextRND();
  return __MODF(ooc2RandomNumbers_z, range);
}

/*----------------------------------------------------------------------------*/
SHORTREAL ooc2RandomNumbers_Random (void)
{
  ooc2RandomNumbers_NextRND();
  return (SHORTREAL)((REAL)(ooc2RandomNumbers_z - 1) * 4.6566128774142013e-10);
}

/*----------------------------------------------------------------------------*/

export void *ooc2RandomNumbers__init (void)
{
  __DEFMOD;
  __REGMOD("ooc2RandomNumbers", 0);
/* BEGIN */
  ooc2RandomNumbers_z = 1;
  __ENDMOD;
}
