please dont rip this site

PIC Microcontoller Math Methods

Quick little 8 bit random number generators

Andrew Warren [fastfwd at] 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:
    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:

    MARV:   MOVLW   01DH
            RLF     RANDOM
            XORWF   RANDOM
            RETLW   0

From Robert LaBudde and Nikolai Golovchenko

;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
                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




See also:

file: /Techref/microchip/rand8bit.htm, 4KB, , updated: 2010/4/29 10:42, local time: 2017/7/24 03:29,

 ©2017 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE! / 

<A HREF=""> PIC Microcontoller Math Methods - Quick little 8 bit random number generators</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2017 contributors:
o List host: MIT, Site host, Top posters @20170724 RussellMc, Van Horn, David, IVP, James Cameron, Sean Breheny, alan.b.pearce, Neil, Bob Blick, David C Brown, Denny Esterline,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!