Full Version : Quick 8-Bit Random Number Generators (PIC ASM)
avr >>MATH ROUTINES >>Quick 8-Bit Random Number Generators (PIC ASM)


Admin3- 04-18-2006
Quick little 8 bit random number generators

Andrew Warren [fastfwd at ix.netcom.com] of Fast Forward Engineering - San Diego, California says

Load a register called "RANDOM" with any non-zero value, then call this routine each time you'd like a new pseudo-random value:
CODE

   LFSR:   RLF     RANDOM,W
           RLF     RANDOM,W
           BTFSC   RANDOM,4
           XORLW   1
           BTFSC   RANDOM,5
           XORLW   1
           BTFSC   RANDOM,3
           XORLW   1
           MOVWF   RANDOM
           RETLW   0

Scott Dattalo says:

[with the double RLF at the start of this routine,] Andy is implementing 'roll left' where the most significant bit of RANDOM will get copied to least significant position. This is how it works. The first RLF will copy the most significant bit of RANDOM into the carry. What ever was in the carry prior to the first RLF will get copied into the least significant bit position - but we don't care. Also, since the destination is the W register, the variable RANDOM is unaffected. The second RLF repeats the same rotate operation, but this time the carry has been initialized to the MS bit of random. So this second rotate will copy the MS bit into the least significant bit. All of the other bits are of course shifted left one bit position. See?
Or, if you prefer, you can use this routine (written by Marv Isaacman) instead:
CODE

   MARV:   MOVLW   01DH
           CLRC
           RLF     RANDOM
           SKPNC
           XORWF   RANDOM
           RETLW   0

From Robert LaBudde and Nikolai Golovchenko
CODE

Randomize
;Rnew = Rold * 221 + 53
;221 = 256 - 32 - 4 + 1
;256 can be eliminated
;so we need to calculate Rnew = Rold * (1 - 32 - 4) + 53 using
;truncating arithmetic
;or Rnew = Rold * (-32 - 3) + 53
               clrc
               rlf     Number, 1
               swapf   Number, 0
               andlw   0xE0
               rrf     Number, 1
               addwf   Number, 0
               addwf   Number, 0
               addwf   Number, 0
               sublw   0x35
               movwf   Number


See piclist post 2000\12\17\211923a

Mark Jeronimus of Digital Mosular shares this code:
These are implementations of the Galois LFSR (Linear Feedback Shift Register)
8-bit random generator (tested)

The next one is not a funtion. Place it in the code where the random number is needed or you can put it in a function yourself. The prime LFSR polynom is 0xB4.
CODE

   BCF     STATUS,C    
   RRCF    LFSRVALUEL,W
   BTFSC   STATUS,C
   XORLW   0xB4
   MOVWF   LFSRVALUEL

16-bit random generator (tested)

The 0xA1 is part of a prime polynom value 0xA1A1 which generates a full LFSR
CODE

Random:
   BCF     STATUS,C    
   RRCF    LFSRVALUEH,F
   RRCF    LFSRVALUEL,F
   BTFSS   STATUS,C
   RETURN
   MOVLW   0xA1
   XORWF   LFSRVALUEH
   XORWF   LFSRVALUEL    
   RETURN

24-bit random generator (tested)

The prime polynom is 0xD7D7D7
CODE

Random:
   global Random

   BCF     STATUS,C    
   RRCF    LFSRVALUEU,F
   RRCF    LFSRVALUEH,F
   RRCF    LFSRVALUEL,F
   BTFSS   STATUS,C
   RETURN
   MOVLW   0xD7
   XORWF   LFSRVALUEU
   XORWF   LFSRVALUEH
   XORWF   LFSRVALUEL    
   RETURN


32-bit random generator (tested)

The prime polynom is 0xA6A6A6A6 (Yes this is a prime polynom! It went through a hell to find it)

CODE

Random:
   BCF     STATUS,C    
   RRCF    LFSRVALUEV,F
   RRCF    LFSRVALUEU,F
   RRCF    LFSRVALUEH,F
   RRCF    LFSRVALUEL,F
   BTFSS   STATUS,C
   RETURN  
   MOVLW   0xA6
   XORWF   LFSRVALUEV
   XORWF   LFSRVALUEU
   XORWF   LFSRVALUEH
   XORWF   LFSRVALUEL    
   RETURN



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.