Path: news.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!uwm.edu!cs.utexas.edu!sun-barr!ames!data.nas.nasa.gov!taligent!apple!dlyons From: dlyons@Apple.COM (David A Lyons) Newsgroups: comp.sys.apple2 Subject: Re: Random Number Generator Needed! Message-ID: <74636@apple.apple.COM> Date: 24 Nov 92 04:48:32 GMT References: <1992Nov19.154845.2806@brandonu.ca> Organization: Apple Computer Inc., Cupertino, CA Lines: 45 In article <1992Nov19.154845.2806@brandonu.ca> marshall@brandonu.ca (Edward S. Marshall) writes: >Yet another plea from a desperate psuedo-BASIC programmer: > >I'm in need of a SHORT and SIMPLE random-number generator for ML. Something >that'll return a number between $00 and $FF at a variable memory location. >I've tried using a few locations for a seed, but nothing seems to come up >random enough. Basically, I'm stuck. Anyone who's got some code that can >help me out, PLEASE write! If it helps, I'm running on an e//e. Thanks in >advance! Counting the time until the VBL signal inverts (bit 7 of $C019) is one possible way. Works on the GS and IIe at least...not sure about the IIc or IIc Plus offhand. The system randomizes $4E and $4F while waiting for the user to hit a key. You might be able to use that. If you're looking for a pseudo-random algorithm, here's my favorite. It cycles through all 255 nonzero values before repeating (just be sure not to seed it with zero). lda num asl a bcc done eor #$87 done sta num Simple, no? Shift it left a bit; if you shifted out a 1 bit, exclusive or with $87, and the result is also the seed for next time. To jumble the order better (so you don't get 1, 2, 4, 8, 16, 32, 64, 128 in order, for example, you can call the whole thing 11 times each time you want a number, and you still get all 255 values before it repeats). For 16 bits, the mask is $1D87, and you can repeat 19 times for more jumbled results (there are longer masks that I don't remember offhand, and in general the jumble-count is 3 more than the number of bits in your values). Source: An article in Popular Electronics, probably in the early 80s. I should dig that up sometime. -- David A. Lyons, Apple Computer, Inc. | DAL Systems Apple II System Software Engineer | P.O. Box 875 Internet:dlyons@apple.com | Cupertino, CA 95015-0875 My opinions are my own, not Apple's. Path: news1.icaen!news.uiowa.edu!NewsNG.Chicago.Qual.Net!128.174.5.49!vixen.cso.uiuc.edu!logbridge.uoregon.edu!newsfeed.berkeley.edu!newsfeed.acns.nwu.edu!news.acns.nwu.edu!merle!judd From: judd@merle.acns.nwu.edu (Stephen Judd) Newsgroups: comp.sys.apple2.programmer Subject: Re: Random numbers from assembly Date: 11 May 1999 18:49:23 GMT Organization: Northwestern University, Evanston, IL Lines: 25 Message-ID: <7h9u3j$7je@news.acns.nwu.edu> References: <7h7hva$ju2$1@nnrp1.deja.com> Reply-To: sjudd@tired.of.spam.nwu.edu (Stephen Judd) NNTP-Posting-Host: merle.acns.nwu.edu Xref: news1.icaen comp.sys.apple2.programmer:10590 Hola Bob, In article <7h7hva$ju2$1@nnrp1.deja.com>, wrote: >How do I get a good random number from assembly? >This is for a game. There are several 6502 generator routines at http://www.ffd2.com/fridge/ Moreover, you might try the "middle-squares" method, which dates back to von Neumann. In this method, you get the next number in the sequence by squaring the current number and taking the middle n/2 digits. For example, for an 8-bit number, you square the current number, giving a 16-bit number, and use the middle 8-bits (divide by 16). This can be implemented with a single table. You can use the clock or some other convenient spot for the initial seed. The problem with this method is that there are several short-cycles, i.e. certain initial conditions will give very poor sequences. One way to get around this is to reset the generator after a certain number of iterations; you can also reset if x_n+1 = x_n or if x_n = 0. I have recently been experimenting with this method, and it seems to work reasonably well. -Steve