[英]Does the following code actually implement the LCG algorithm in assembly
我听说在 Logical Congruential Generator 算法中,我们应该使用我们之前生成的数字来生成一个新的数字。 但是,我发现了以下代码:
MOV AH, 00h ; interrupt to get system timer in CX:DX
INT 1AH
mov [PRN], dx
call CalcNew ; -> AX is a random number
xor dx, dx
mov cx, 10
div cx ; here dx contains the remainder - from 0 to 9
add dl, '0' ; to ascii from '0' to '9'
mov ah, 02h ; call interrupt to display a value in DL
int 21h
call CalcNew ; -> AX is another random number
...
ret
; ----------------
; inputs: none (modifies PRN seed variable)
; clobbers: DX. returns: AX = next random number
CalcNew:
mov ax, 25173 ; LCG Multiplier
mul word ptr [PRN] ; DX:AX = LCG multiplier * seed
add ax, 13849 ; Add LCG increment value
; Modulo 65536, AX = (multiplier*seed+increment) mod 65536
mov [PRN], ax ; Update seed = return value
ret
我看到它每次都使用系统时间,而不是以前的数字。 我对吗? 我正在使用 TASM。
CalcNew
函数正确使用了先前的种子。 只需查看CalcNew:
标签和ret
之间的代码。
AH=0 / int 1AH
+ mov [PRN], dx
是 LCG 的种子。
在整个程序中执行一次,就像在代码中一样。
对于以后的调用,只需call CalcNew
(并将结果处理到您想要的任何范围内)。 请注意,调用代码在第一次调用之前使用当前时间的低位作为种子,但总共进行了两次调用。 它不会在它们之间重新播种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.