I do not know whether the PRNG I use in **fbForth **(same as TI Forth) is any better than what you are using, but here it is:

The _RNDW function first retrieves the seed from >83C0. Then the result of seed * 6FE5h + 7AB9h is shifted right circularly by 5 bits. This result is stored at >83C0 as the new seed and also returned to the user:

**_RNDW LI R0,>6FE5 calculate new pseudo-random **

** MPY @>83C0,R0 seed * >6FE5**

** AI R1,>7AB9 seed * >6FE5 + >7AB9**

** SRC R1,5 (seed * >6FE5 + >7AB9) SRC 5**

** MOV R1,@>83C0 store new seed**

** B *R11 return to caller**

The _RND function returns a positive integer less than the number supplied to it, which includes 0. _RND invokes _RNDW , takes the absolute value of the result and divides by the user-supplied number and returns the remainder. Obviously, for your case, retrieving only the LSB would be a much more economical operation than division:

**_RND MOV R11,R15 save return**

** BL @_RNDW get random number into R1**

** ABS R1 force positive**

** CLR R0 set up for division**

** DIV *R9,R0 divide number in R0--R1 by num on stack**

** MOV R1,*R9 return remainder on stack**

** B *R15**

The above done your way:

**RND256 MOV R11,R15 save return**

** BL @_RNDW get random number into R1**

** ANDI R1,255 extract right byte (MOD 256)**

** MOV R1,*R9 return remainder on stack**

** B *R15**

I have puzzled over changing **RND** for **fbForth 2.0** to not do the ABS and to just return all but the MSb, i.e., leftmost bit. I should compare the distribution of pseudo-random numbers done both ways, I suppose.

One final note, randomizing the seed in both TI Forth and **fbForth **counts from zero until a VDP interrupt is detected. Though it sets up a race condition, I have never had any trouble with it:

**_RNDMZ MOVB @>8802,R0 get VDP status byte**

** CLR R0 discard it**

** CLR R1 clear counter**

**RZLOOP INC R1 increment counter**

** MOVB @>8802,R0 get VDP status byte**

** ANDI R0,>8000 VDP interrupt?**

** JEQ RZLOOP no, increment counter**

** MOV R1,@>83C0 yes, store new seed**

** B *R11**

...lee