Searching \ for 'Pseudo Random Number Generator Question' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/method/math.htm?key=random
Search entire site for: 'Pseudo Random Number Generator Question'.

Truncated match.
PICList Thread
'Pseudo Random Number Generator Question'
1996\06\29@185216 by Paul J. Miller

picon face
Dear All,
       I am new to this list so excuse me if I bring up a subject which
may have been discussed recently, I am still digesting the LISTSERV
Database Functions Document.

       I am working on a project which needs to generate Random
Numbers, 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?

       The background, I am using a 16C61 running at 12.288MHz to
generate audio sine waves and also white noise via an R-2R ladder
network ( a DAC ) connected to port B, port A is an input which
determines what the chip should do. The sine wave generation is working
fine I can get any frequency from sub-audio to 22kHz and above in 2 Hz
steps with crystal controlled accuracy, with or without FM. The white
noise generator has problems, it 's spectrum looks like it has been
passed through a comb filter because of the short sequence length.


SREG_1 to SREG_5 are the shift register
SREG_T is temporary workspace
STORE is a copy of the command sent to the PIC

-------------------Here is the code-------------------------------

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_4,W
       MOVWF   PORTB           ; Send noise to the port

       SWAPF   SREG_5,W        ; 35th stage goes into bit 6
       XORWF   SREG_5,W        ; bit 6 is now XOR of 35th and 39th stage
       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

-------------------End of code--------------------------------

       If anyone out there can help I would be very gratefull.
TIA

Regards

Paul J. Miller

1996\06\29@224407 by fastfwd

face
flavicon
face
Paul J. Miller <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> 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 - .....fastfwdKILLspamspam@spam@ix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499

More... (looser matching)
- Last day of these posts
- In 1996 , 1997 only
- Today
- New search...