Path: csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!Xl.tags.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 04 Mar 2016 09:47:13 -0600 From: Scott Hemphill Newsgroups: comp.sys.apple2.programmer Subject: Re: Streamlining Applesoft - Random number generators References: <7dd16923-704e-4054-83f2-71be03ae9b92@googlegroups.com> <90a30bbe-9288-49c6-9728-624896a627bd@googlegroups.com> <754e0a85-0ee0-4b7d-8d97-16f4f60f4b17@googlegroups.com> <99335f72-8384-4cd8-b719-cbbe86e7b130@googlegroups.com> <0d7e1711-4586-4203-9825-5229a1c64b08@googlegroups.com> <6d52de94-fc33-423d-abdb-fad2ec5af67e@googlegroups.com> <7NadnThcNNnNrUTLnZ2dnUU7-KfNnZ2d@giganews.com> Reply-To: hemphill@alumni.caltech.edu Date: Fri, 04 Mar 2016 10:47:13 -0500 Message-ID: <87ziue2qke.fsf@hemphills.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:AYJLS19Fa0QUS54qff9WyzNyFqU= MIME-Version: 1.0 Content-Type: text/plain Lines: 95 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-3uI5OmYSLVkZQ77DumpgnbfVAoXc1ksa/FvpQUycmTCtG7H4iTzSb67hMqNKOohHlm4Bc9s/li/yHPi!FeC3lzID5mebjv7O/AXyYrE0mjMfiwuqU0Zg6vAS/hhOajWBTDq9uMw= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5048 X-Received-Bytes: 5160 X-Received-Body-CRC: 3468296201 Xref: csiph.com comp.sys.apple2.programmer:2429 Michael J. Mahon writes: > wrote: >>>> Thanks Michael! That's *exactly* the code I was thinking of -- just >>>> couldn't remember the name (or location). >>>> >>>> Pity that AppleSoft doesn't use the 16-bit RNDL and RNDH as part >>>> of its seed. :-/ >>> >>> When debugging, you want deterministic pseudorandom numbers. You can >>> always PEEK the GETCHR counter and use it to seed RND if you want a >>> different set each time. >>> >>> By default, RND delivers the same sequence each time after a cold start >>> (assuming that the uninitialized low byte of the seed powers up in the same >>> state ;-). >>> >>> If you really want to be sure the same sequence occurs on each run, you >>> should seed RND at the start of your program, though a good fraction of >>> seeds seem to deliver a modest pseudorandom sequence followed by a short >>> cycle... >> >> >> For the IIGS anyways, I think the VBL and HBL would be an excellent >> source for a seed on startup. These values are always random and when >> used in conjunction with RNDL and RNDH can give a 4-byte random seed, and >> you can even give a random key press for a 5th byte, different every >> time on bootup. >> >> I use this for a 5 byte random seed in Sweet 16 and GSPORT and real IIGS. >> I believe this should work in a IIc as well but not II+ or IIe's >> standard or enhanced. >> >> ]LOOP INC $4E >> BNE NOTHI >> INC $4F >> NOTHI BIT $C000 >> BPL ]LOOP >> LDA $C010 >> STA $C9 >> LDX $C02E >> LDA $C02F >> STX $CD >> STA $CC >> LDA $4E >> LDX $4F >> STA $CB >> STX $C9 >> ... >> >> >> test to see if your computer's VBL or HBL gives values with this: >> >> 300: LDX $C02E >> LDA $C02F >> JSR $F941 >> BIT $C000 >> BPL $300 >> BIT $C010 >> RTS >> > > When you seed a random number generator you are only selecting a point in > the generators cycle where you want to start. > > A good generator will have a very long cycle, but every generator has at > least one cycle, and a good generator has only one, maximal length cycle. > > So if you have a two-byte seed, you are randomly selecting one of 2^16 > starting points in the cycle. A longer seed does not make the numbers more > random, it just selects from a larger number of starting points. The > quality of a PRNG is not determined by its seed, but only by its algorithm. > > > I'd guess that 2^16 different sequences of pseudorandom numbers is more > than enough for any application. > > In the case of Applesoft's RND generator, it is so riddled with short > cycles that seeding with a random (and therefore untested) seed has a high > probability of producing a short, degenerate sequence. Better to select > from a small set of seeds that have been verified to generate at least a > few thousand different numbers. > > Of course, it's actually best to avoid RND entirely for any serious work. I used a high-quality linear congruential generator in my 3D tic-tac-toe program. I was just going to say something about it, and I found this URL in my program comments: http://www.6502.org/source/integers/random/random.html Scott -- Scott Hemphill hemphill@alumni.caltech.edu "This isn't flying. This is falling, with style." -- Buzz Lightyear