Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: kegs@provalid.com (Kent Dickey) Newsgroups: comp.sys.apple2.programmer Subject: Re: Linear Feedback Shift Register Date: Thu, 5 Sep 2024 20:13:09 -0000 (UTC) Organization: provalid.com Lines: 81 Message-ID: References: Injection-Date: Thu, 05 Sep 2024 22:13:10 +0200 (CEST) Injection-Info: dont-email.me; posting-host="de08fa5dbc9a95f31b2ff3ebde359a57"; logging-data="503763"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+q9wGXqTpCBe8LrMimTKy9" Cancel-Lock: sha1:tDT32zJ6IQUAfnrU75L2V64pTow= X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Originator: kegs@provalid.com (Kent Dickey) Xref: csiph.com comp.sys.apple2.programmer:6315 In article , Duhast wrote: >I'm trying to generate pseudo random numbers 1-255(yes zero is a special >case). Following Graphic Gems, and code I found at codebase64 I came up >with this: > > ORG $300 > > LDY #$00 > >XOR LSR RAND > LDA RAND > BCC SKIP > EOR #$B8 >SKIP STA RAND > STA $2000,Y > INY > BEQ DONE > JMP XOR > >DONE RTS > >RAND DB $01 > >I then put the values in text file: > > 10 D$ = CHR$ (4) > 20 PRINT D$;"OPEN RAND" > 30 PRINT D$;"WRITE RAND" > 40 FOR I = 0 TO 255 > 50 PRINT PEEK (8196 + I) > 60 NEXT > 70 PRINT D$;"CLOSE" > >Pasted them into a spreadsheet and sorted. It doesn't work. There are >missing numbers, duplicate numbers. What am I doing wrong? Is there a >better method? You should use PEEK(8192+I). That may be the only problem. And you can optimize the routine a bit: LOOP LDA RAND LSR BCC SKIP EOR #$B8 SKIP STA RAND STA $2000,Y INY BNE LOOP RTS You don't have to save RAND in a memory location at all, just remove the LDA RAND and STA RAND. It's important for LFSR to start at a non-0 number (if you start it with 0, it stays at 0), so I'd initialize RAND to 1 before the loop. LDA #1 STA RAND You can see it works with a more elaborate code which counts the occurrence of each byte at output: LDY #0 LDA #0 CLR STA $4000,Y INY BNE CLR LDA #1 LOOP LSR BCC SKIP EOR #$b8 SKIP TAX INC $4000,X INY BNE LOOP RTS Where $4000-40ff will have $01 in every position except $4000, and $40B8 will have $02 (something had to occur twice since $00 is not an output). Kent