【发布时间】:2017-08-09 12:07:17
【问题描述】:
我试图在汇编中创建一个完整的随机数,但每次我启动程序时,它都会以相同的顺序给我相同的数字。 如果数字是 12、132、4113 或其他数字,我每次启动代码时都会重复它们。
我正在尝试制作的程序类似于猜谜游戏。
IDEAL
MODEL small
STACK 100h
DATASEG
;vars here
RNG_Seed dw ?
CODESEG
; Generates a pseudo-random 15-bit number.
; Parameters: <none>
; Clobbers: AX, DX
; Returns: AX contains the random number
proc GenerateRandNum
push bx
push cx
push si
push di
; 32-bit multiplication in 16-bit mode (DX:AX * CX:BX == SI:DI)
mov ax, [RNG_Seed]
xor dx, dx
mov cx, 041C6h
mov bx, 04E6Dh
xor di, di
push ax
mul bx
mov si, dx
xchg di, ax
mul bx
add si, ax
pop ax
mul cx
add si, ax
; Do addition
add di, 3039h
adc si, 0
; Save seed
mov [RNG_Seed], di
; Get result and mask bits
mov ax, si
and ah, 07Fh
pop di
pop si
pop cx
pop bx
ret
endp GenerateRandNum
我可以怎样做才能在每次运行时获得不同的随机数?
【问题讨论】:
-
您的程序是否在没有操作系统的情况下运行?
-
@James 你在操作系统中是什么意思?
-
操作系统
-
对于 PRNG,您可能希望使用更像 xorshift+ 的东西,以避免需要 32 位乘法。 OTOH,找到不适合 32 位版本的常量并非易事。 The reference version 用于 64 位整数。 (您可以只实现 64 位版本以获得相当高质量的 PRNG,尤其是如果您可以使用 386 SHRD / SHLD 来提高扩展精度移位的效率。)
-
@PeterCordes 该论文也有 32 位的常量。见,例如here
标签: assembly random x86 x86-16