Paul J. Miller wrote: > I have made a pseudo random number generator using a 5 byte > shift register and doing an XOR of the 35th and 39th stages to > generate the feedback ( see code below ). According to Horowitz and > Hill this should go through 2 billion shifts before it starts > repeating, my implimentation goes through 1023 states before > repeating and I don't know why. Can anyone see anything wrong with > the code? Alternatively does anyone have known working pseudo random > number generation code which they would be willing to share? Paul: Even assuming that your book is right and that (39,35,0) will give a 2-billion-element sequence, your code has some problems. Try this, instead (new instructions are in lowercase, old unneeded instructions are marked with ";;;;"): NOISE BSF STATUS,C ; Make sure there is at least one 1 NLOOP RLF SREG_1,F ; shift the register RLF SREG_2,F RLF SREG_3,F RLF SREG_4,F ;;;;RLF SREG_5,F MOVF PORTA,W ; Is PORTA still the same? BTFSC STATUS,Z GOTO INIT ; If PORTA = 0 then Restart SUBWF STORE,W BTFSS STATUS,Z GOTO INIT ; If not then go back to the beginning MOVF SREG_1,W xorwf sreg_3,w ; (Don't XOR with SREG_4, since it's onl y ; 7 bits wide) ;;;;XORWF SREG_4,W MOVWF PORTB ; Send noise to the port rlf rand4,w ; bit 7 of w contains 31st stage movwf sreg_t ; store it temporarily swapf rand4,w ; bit 7 of w contains the 28th stage xorwf sreg_t ; bit 7 of sreg_t holds 31st XOR 28th ;;;;SWAPF SREG_5,W ; 35th stage goes into bit 6 ;;;;XORWF SREG_5,W ; bit 6 is now XOR of 35th and 39th ;;;;MOVWF SREG_T ; put it aside for later ;;;;RLF SREG_T,F ; put bit 6 into bit 7 RLF SREG_T,F ; put bit 7 into carry GOTO NLOOP ; go and shift it into the register This routine will give you the 2-billion-element sequence that you were looking for (with one fewer byte of shift-register space). Even so, it may not be suitable for whatever audio application you have... Depending upon your needs, a pseudo-random number generator with better spectral distribution might be more appropriate. -Andy Andrew Warren - fastfwd@ix.netcom.com Fast Forward Engineering, Vista, California http://www.geocities.com/SiliconValley/2499