The following code implements a linear feedback register type of pseudo-random number generator. This algorithm is frequently implemented in hardware for spread-spectrum radio applications. It will generate all possible bit combinations EXCEPT all zeroes, for various lengths, if the value is Poly is chosen correctly, and Variable is initialized with any non-zero number. In other words, it will produce a "maximal length sequence", in which each of 2^n-1 nonzero bit combinations appear once and only once. If Variable is initialized with 0, then it just produces lots of zeroes. This macro was used in an application where a pseudo-random stream of bits was needed, hence its output of a bit in CY. If you want up to an 8-bit value, it will be contained in Variable after executing the macro. Suitable values for Poly: for 6 bits (sequence length 63): 0x21, 0x33, 0x36 for 7 bits (sequence length 127): 0x41, 0x44... for 8 bits (sequence length 255): 0x8e, 0xb4... For more suitable Poly values see: Dixon, Robert C., Spread Spectrum Systems with Commercial Applications; (Table 3.7) ;------------------------------------------------------------ ; GenerateLRC Macro -- generates up to 8-bit LRC ; ; Input: Variable -- needs to be initialized (usually all ones to # of bits) ; ; Output: Next output bit is in CY, Variable is updated for next cycle ; ;------------------------------------------------------------ GenerateLRC Macro Poly, Variable clrc rlf Variable,f movlw Poly skpnc xorwf Variable,f endm > -----Original Message----- > From: Ron Anthony [mailto:ronant@OPTONLINE.NET] > Sent: Saturday, August 18, 2001 10:37 PM > To: PICLIST@MITVMA.MIT.EDU > Subject: [PIC]: Pseudo-Random numbers > > > Hello all. I need to generate numbers that appear to be > ranomized, it makes > no difference if they are truly random. I can seed it with a > value culled > from ram before i wipe the ram, but it's not necessary. > > Any simple code to generate a seemingly random number, from 0 to 255? > > -- > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > > -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads