Path: 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 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 -- ---------------------------------------------------------------------- Leonard Hamey Computer Science Dept. lgh@cs.cmu.edu Carnegie Mellon University