SX Microcontroller Math Method

An 8bit software random number generator.

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:
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	#0

Scott 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

Hardware white noise generator

, 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''' = 255-RANDOM'' = -1 - RANDOM''
        mov     W, #54
        add     RANDOM, W               ;RANDOM''''=-1 - 32*RANDOM - 3*RANDOM + 54

See also: