Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!julius.cs.uiuc.edu!psuvax1!psuvm!cunyvm!byuvm!byuvax!yoda!eeta From: eeta@yoda.byu.edu Newsgroups: comp.sys.apple2 Subject: Re: random no. in assembly? Message-ID: <382eeta@yoda.byu.edu> Date: 31 Jan 91 00:14:49 GMT Lines: 52 >>Hi all. I am looking for a way to generate random numbers in an assembly >>language program. The entry point to the applesoft RND function would >>be a start, but I really just need an algorithm. I'm using one that >>shifts a 2-byte seed left once and adds it back to the seed. that doesn't >>give very good results after the first few numbers. > >I wrote a simple one that basically does a CRC-16 (I can send you a >6502 CRC-16 calculator) on values grabbed from somewhere on page $C0 >(something like $C022 or something). These softswitches usually have >random changing garbage in them, so they supply numbers about as >random as you can get. > The problem with this (at least on older II's) is that these registers' so called "random numbers" are actually the current video line being scanned and therefore have a limited range. (This fact was used in some of the higher quality games of the past to avoid flicker by waiting for the vertical blanking time. With the //e, a specific register was set up to do this.) So, it seems to me that these numbers would make a good seed, but then some code like this would make a good random algorithm: RND EPZ $xx ; Random number base address -- some location ; in zero page, you need 6 contiguous bytes RANDOM CLD SEC LDA RND+1 ADC RND+4 ADC RND+5 STA RND LDX #$04 ^1 LDA RND,X STA RND+1,X DEX BPL <1 RTS I used that code chunk in a game I wrote about 6 years ago called "Rat Patrol". It returns a random number between 0 and 255 in the accumulator. If you need more bits precision than 8, you can take the bytes in RND+x. (I've never had a use for more than 8 bits of precision, and I find this code to be pretty good- it doesn't lock up and has what appears to be a good distribution.) To seed the random numbers, put a seed in RND at least. One good way to seed it that I use is to increment RND, RND+, .... , RND+5 as if they were a 48-bit counter while waiting for a user to press a key. Or you could get your seed from one of the unused I/O registers as mentioned above... Well, anyway, that's my $0.02. (Take it or leave it, etc., etc.) Happy random number generating! (Oh, BTW, I'm looking for a 3.5" disk drive and controller for a //e, if anyone wants to unload one...) -- Don Yacktman eeta@yoda.byu.edu