🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac
Article #23144

Re: Lightspeed C bug?

#23144
From: Leonard Hamey
Date: Thu, 24 Dec 1987 15:02
38 lines
1505 bytes
In article <870081@hpcilzb.HP.COM> cnc@hpcilzb.HP.COM (Chris Christensen) writes:
>I have been having a real head-scratcher problem with Lightspeed C.
>
>I am trying to generate random numbers from 0 to 7.
>
>I use
>
>#define ABS(x) (((x) < 0) ? -(x) : (x))
>x = ABS(Random()) % 8;
>
>I get random numbers from -7 to 7
>

Expand the macro and you get....

	x = (((Random()) < 0) ? -(Random ()) : (Random ())

each invocation of Random() generates a NEW random number, so you are
using one random number to determine whether or not to negate another random
number.

I would expect that your best (hack) answer is to use:

	x = Random()&7;

or

	x = Random()&~(-1) % N;
		for randoms on 0..N-1.
----------------------------------------------------------------------
Leonard Hamey			Computer Science Dept.
lgh@cs.cmu.edu			Carnegie Mellon University
<Substitute your favourite disclaimer here>
-- 
----------------------------------------------------------------------
Leonard Hamey			Computer Science Dept.
lgh@cs.cmu.edu			Carnegie Mellon University
<Substitute your favourite disclaimer here>
Technical Details
Message-ID: <573@PT.CS.CMU.EDU>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!bbn!rochester!PT.CS.CMU.EDU!IUS2.CS.CMU.EDU!lgh
References: <870081@hpcilzb.HP.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!bbn!rochester!PT.CS.CMU.EDU!IUS2.CS.CMU.EDU!lgh
From: lgh@IUS2.CS.CMU.EDU (Leonard Hamey)
Newsgroups: comp.sys.mac
Subject: Re: Lightspeed C bug?
Message-ID: <573@PT.CS.CMU.EDU>
Date: 24 Dec 87 15:02:30 GMT
References: <870081@hpcilzb.HP.COM>
Sender: netnews@PT.CS.CMU.EDU
Organization: Carnegie-Mellon University, CS/RI
Lines: 38