In SX Microcontrollers, SX/B Compiler and SX-Key Tool, PJMonty wrote: Electro, Here's the routine I got from the SxList: [code];--------------------------------------------- ; 7/02/04 PJM - This function uses a linear congruential ; sequence generator as a pseudo-random number generator. RandomNumGen ;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 RandomNum ;RandomNum' = 2*RandomNum and carry contains the original MSb mov W, <>RandomNum and W, #$E0 ;W = 16*RandomNum'=32*RandomNum rr RandomNum ;restore RandomNum add W, RandomNum add W, RandomNum add RandomNum, W ;RandomNum'' = 32*RandomNum + 3*RandomNum not RandomNum ;RandomNum''' = 255-RandomNum'' = -1 - RandomNum'' mov W, #54 add RandomNum, W ;RandomNum''''=-1 - 32*RandomNum - 3*RandomNum + 54 [/code] "RandomNum" is a variable (in this case it's a global variable) that holds the value of most recent random number generated between interations. Every time you call this routine, you'll update the value of "RandomNum". If you want to find the original, search for "congruential" on the SxList site. Make sure you go to the page with the SX code and not the PIC code. [list]Thanks, PeterM[/list] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90121 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)