C489030@UMCVMB.MISSOURI.EDU (Greg Hodgdon) (01/30/91)
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. thanks greg c489030@umcvmb.missouri.edu
dcw@lcs.mit.edu (David C. Whitney) (01/30/91)
In article <9101300829.AA21626@apple.com> C489030@UMCVMB.MISSOURI.EDU (Greg Hodgdon) writes: >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. -- Dave Whitney Computer Science MIT 1990 | I wrote Z-Link and BinSCII. Send me bug dcw@lcs.mit.edu dcw@mit.edu | reports. I have a job. Don't send me offers. "The price of freedom is eternal vigilance" --Binky (aka Matt Groening)
eeta@yoda.byu.edu (01/31/91)
>>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