Replace the random number generator

Decimal BASIC Ver 7.2 or earlier had generated the random number generated by the multiplicative congruential method with modulo 2147483647, multiplier 16807.
You can reproduce those random numbers as follows.

100 OPTION ARITHMETIC NATIVE
110 DECLARE EXTERNAL FUNCTION math.random
120 DECLARE EXTERNAL SUB math.randomize
130 CALL randomize
140 FOR i=1 TO 10
150    PRINT random
160 NEXT i
170 END
10000 MODULE math
10010 MODULE OPTION ARITHMETIC NATIVE
10020 PUBLIC FUNCTION random
10030 PUBLIC SUB randomize
10040 SHARE NUMERIC seed,a,m
10050 LET seed=54321         
10060 LET a=16807
10070 LET m=2147483647
10080 EXTERNAL FUNCTION random
10090    LET seed=MOD(a*seed,m) 
10100    LET random=seed/m
10110 END FUNCTION 
10120 EXTERNAL SUB randomize
10130    DECLARE NUMERIC x
10140    LET seed =ROUND(100*TIME)+1 
10150    LET x=random       ! discard one random number (not required)
10160 END SUB
10170 END MODULE


Another type of pseudorandom number generator can be tested in a similar way.
The essential point is to use a MODULE to have a variable whose value remains after the invocation.

For the way how to generate random numbers using a general DLL, refer here.


Back