Because this topic has surfaced in several threads on this forum, I decided to give it its own thread and explain in great detail how TI Basic’s RND function and the GPL RAND function it calls 7 – 69 times work.
PRN = SEED * 28645 + 31417
RAND then stores the PRN thus generated back into >83C0 as the new seed. RAND next adds 1 to the limit value it was passed to use as the modulus to operate on the PRN just generated after its bytes are swapped (now PRN'). The number returned in >8378 = PRN' modulo (lim+1).
*-------- GENERATE A PSEUDORANDOM NUMBER ---------- * WKSC EQU >83C0 ISR workspace (R0 = PRN SEED) WKSE EQU >83E0 GPL workspace R5LSB EQU WKSE+R5+R5+1 low byte of R5 of GPL WS RANDOM EQU >8378 return location for RAND's PRN return byte RAND LI R4,>6FE5 28645 to R4 MPY @WKSC,R4 SEED * 28645 AI R5,>7AB9 PRN = SEED * 28645 + 31417 MOV R5,@WKSC PRN is stored as new SEED in >83C0 MOVB *R13,R6 load limit number (byte) from caller to R6 SRL R6,8 shift to LSB INC R6 make modulus (lim+1) CLR R4 clear upper half of dividend SWPB R5 swap bytes of PRN to get PRN' DIV R6,R4 perform division by modulus (lim+1) MOVB @R5LSB,@RANDOM store LSB of remainder in >8378 B @NEXT return to caller for next GPL instruction
RND ST @>834A,>3F radix-100 exponent = -1 to FAC ST @>8310,>4B loop counter for significand starting at FAC+1 RND1 RAND >63 get random radix-100 digit to >8378 CZ @>8378 0? BR GROM@RND3 no, keep current exponent and go on DEC @>834A decrement exponent in FAC CZ @>834A 0? BS GROM@RND4 exit with 0 as random number BR GROM@RND1 back to exponent manipulation RND2 RAND >63 get random radix-100 digit to >8378 RND3 ST *>8310,@>8378 all 7 radix-100 digits (FAC+1 to FAC+7) CEQ @>8310,>51 till >8351 (FAC+8) BS GROM@RNDX exit if beyond end of number INC @>8310 increase loop counter BR GROM@RND2 back to get next random digit RND4 CLR @>834B set 0 RNDX CONT return to TI Basic