#ifndef lint
static char *RCSid = "$Header: /usr/brule/guest/empire/empire/emprcs/lib/gen/chance.c,v 2.0 1995/07/27 05:56:17 empire Exp $";
#endif

/*
 * chance.c
 *
 * return 1 if "roll" is under the chance
 *
 * from PSL Empire, 1985
 */

#ifdef hpux
srandom(n)
	long	n;
{
	extern void srand48();

	srand48(n);
}

long
random()
{
	extern	long lrand48();

	return (lrand48());	/* 5/28/91 by bailey@mcs.kent.edu */
}
#endif

int
chance(d)
	double	d;
{
	double	roll;

	roll = (random() & 0x7fff);

	if (d >  roll/32768.0)
		return 1;
	return 0;
}

int
roll(n)
	int	n;
{
	return (random() % n) + 1;
}

/*
 * round value to nearest int (on the average). E.g. rounds up
 * with a chance proportional to the size of the fractional part.
 */
int
roundavg(val)
	double	val;
{
	int	flr;

	flr = (int) val;
	if (val < 0)
		flr -= chance(flr - val);
	else
		flr += chance(val - flr);
	return flr;
}
