Andrew Warren [fastfwd at ix.netcom.com] of Fast Forward Engineering  San Diego, California says
Load a register called "RANDOM" with any nonzero value, then call this routine each time you'd like a new pseudorandom value:LFSR: mov W, <<RANDOM mov W, <<RANDOM snb RANDOM.4 xor W, #1 snb RANDOM.5 xor W, #1 snb RANDOM.3 xor W, #1 mov RANDOM, W retw #0Scott Dattalo says:
[with the double left shift 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
shift will copy the most significant bit of RANDOM into the carry. What ever was in the carry prior to the first shift 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 shift 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: mov W, #$1D clrb C rl RANDOM snb C xor RANDOM, W retw #0
, design by dr. Imre Bartfai, 8/30/1999
+ Vdd  220k 220k 5V  +\/\/\+ +\/\/\++         \   ++ \  EFT317 PNP ++ \    / 220k  C    >+ +  \ \  / \ ++ / 47n  + \  +B 220k /   /    >+ \ \   1/2 LM358 ++ /  E      / \ + ++ + + + / 47k  47n     1/2 LM358 \ \ \ \ \ \  4k7 / 1M / / 2k2 1M / / 2k2 + out \ \ \ \ \      +++++ Vss Standard TTL can not be driven, but CMOS (and maybe LS) are o.k. The power supply must be filtered thoroughly due to the high gain!
Linear congruential sequence generator by 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 clrb C rl RANDOM ;RANDOM' = 2*RANDOM and carry contains the original MSb mov W, <>RANDOM and W, #$E0 ;W = 16*RANDOM'=32*RANDOM rr RANDOM ;restore RANDOM add W, RANDOM add W, RANDOM add RANDOM, W ;RANDOM'' = 32*RANDOM + 3*RANDOM not RANDOM ;RANDOM''' = 255RANDOM'' = 1  RANDOM'' mov W, #54 add RANDOM, W ;RANDOM''''=1  32*RANDOM  3*RANDOM + 54
See also:
file: /Techref/scenix/lib/math/rand_sx.htm, 4KB, , updated: 2004/6/10 14:40, local time: 2018/7/16 15:13,

©2018 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? <A HREF="http://www.piclist.com/techref/scenix/lib/math/rand_sx.htm"> SX Microcontroller Math Method  Random </A> 
Did you find what you needed? 
PICList 2018 contributors:
o List host: MIT, Site host massmind.org, Top posters @20180716 RussellMc, Van Horn, David, Sean Breheny, Isaac M. Bavaresco, David C Brown, Bob Blick, Neil, Denny Esterline, John Gardner, Brent Brown, * 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 RCL1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. ongoing support is MOST appreciated! * Contributors: Richard Seriani, Sr. 
Welcome to www.piclist.com! 
.