[rec.games.hack] nethack bug fix: 16-bit hosts can send you to heaven involuntarily

eggert@sea.sm.unisys.com (Paul Eggert) (01/30/88)

A portability bug in nethack's rnd.c can affect adventurers on 16-bit hosts.
The bug caused an adventurer deep in the dungeon to be sent to heaven
involuntarily by a teleport trap.  His screams of disappointment caused me to
investigate the problem.  Here is a fix that makes integer overflow (and thus
heaven) less likely by a factor of 2^32 on such machines.

***************
*** 43,55 ****
  	return(tmp);
  }
  
! rnz(x)
! register x;
  {
!         register tmp = 1000;
  	tmp += rn2(1000);
  	tmp *= rne(4);
  	if (rn2(2)) { x *= tmp; x /= 1000; }
  	else { x *= 1000; x /= tmp; }
! 	return(x);
  }
--- 43,56 ----
  	return(tmp);
  }
  
! rnz(i)
! int i;
  {
! 	register long x = i;
! 	register long tmp = 1000;
  	tmp += rn2(1000);
  	tmp *= rne(4);
  	if (rn2(2)) { x *= tmp; x /= 1000; }
  	else { x *= 1000; x /= tmp; }
! 	return((int)x);
  }