Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!snorkelwacker!mit-eddie!bu.edu!dartvax!eleazar.dartmouth.edu!llama From: llama@eleazar.dartmouth.edu (Joseph A. Francis) Newsgroups: comp.sys.mac.programmer Subject: Re: Random number Generator wanted. Message-ID: <22950@dartvax.Dartmouth.EDU> Date: 28 Jun 90 20:43:06 GMT References: <152@asacsg.mh.nl> <47XL38V@xavier.swarthmore.edu> Sender: news@dartvax.Dartmouth.EDU Organization: Dartmouth College, Hanover, NH Lines: 35 In article <47XL38V@xavier.swarthmore.edu> jackiw@cs.swarthmore.edu (Nick Jackiw) writes: ->kr@asacsg.mh.nl (Koos Remigius) writes: ->.> This function gives me a random integer ( between 0 and range ). ->.> function Randomize(range:Integer):Integer; ->.> var ->.> rawResult:LongInt; ->.> begin { Randomize } ->.> rawResult:=(ABS(Random); ->.> Randomize:=(rawResult * range) div 32768;{ Scale to specified range } ->.> end; { Randomize } -> ->---------- ->Much more efficient: Randomize:=abs(random) mod succ(range); ->---------- Actually, this may be a dangerous "optimization". Many random number generators yeild numbers which are evenly DISTRIBUTED, but which in other ways are non-random. One very widely used random number generation algorithm generates numbers wholes last digits cycle repeatedly through the same values (in decimal). So, for instance, if you are looking for random numbers in the range 0-9 (pretty common!) you will continually get the same cycle of digits back if you use MOD. If you use DIV, the results will have the same overall "randomness" as the raw numbers returned by the random function. >> Greetings from Holland, >> Koos Remigius. > >Regards from Philadelphia. Hubba hubbas from Hanover. Joe Francis ---------------------------------------------------------------------------- "Read my Lips: No Nude Texans!" - George Bush, clarifying a misunderstanding