在汇编中生成随机数 [英] Generating random numbers in assembly

查看:890
本文介绍了在汇编中生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是汇编新手,在生成随机数时遇到问题.

I am new to assembly, and I am having problems generating random numbers.

我的代码很简单:它在0-25范围内生成100个数字并将它们存储在数组中.

My code is simple: it generates 100 numbers in the 0-25 range and stores them in an array.

我遇到的问题是,当我在emu8086汇编器上运行con时,它将成功运行并生成100个随机数,这些随机数存储在数组中.但是,当我在masm611上运行它时,它每4个周期生成一个新的随机数.这意味着数组中的值对于4个值是连续的相同数字,然后存储下一个随机值.

The problem I am experiencing is that when I run the con on the emu8086 assembler it runs successfully and generates 100 random numbers, that are stored in the array. But when I run it on the masm611, it generates a new random number every 4 cycles. Which means the values in the array are consecutive same number for 4 values and then next random value is stored.

这是我的代码:

.model small
.stack 100h
.data

range db 25
i db 0                  ;iterator

arr db 15 dup(0)        ; an array

.code
   mov ax,@data
   mov ds,ax

   mov bx,offset arr    ;getting the adress of the arr in bx
    L1:

    mov ah,2ch      
    int 21h

    mov ah,0  
    mov al,dl            ;using dl by seeing  2ch details
    div range            ; so the number is in range


    mov [bx],ah          ;ah has remainder as using 8 bits div and  
    inc bx               ;moving to the next index

    inc i
    cmp i,100
    jbe L1


mov ah,4ch               ;returning control
int 21h 
end

我的代码有问题吗?我需要添加一些东西吗?谢谢.

Is there is a problem in my code? Do I need to add something? Thanks.

推荐答案

您的代码的主要问题是它根本不会生成随机数.因为系统时钟不是随机数生成器.我会说,这是非常非随机的数字生成器.

The main problem of your code is that it does not generate random numbers at all. Because the system clock is not random number generator. I would say, it is very non-random number generator.

在程序启动后第一次读取仍然可以被认为是随机的",但前提是您必须在随机的时间手动运行该程序.

The first time read after the start of the program still can be considered "random", but only if you run the program manually in random moment in time.

所有下一个数字将完全不是随机的.

All next numbers will be not random at all.

这样,从系统时钟读取的值适合用作生成(伪)随机数的其他算法的种子(起始值).

This way, the value read from the system clock is suitable for use as a seed (starting value) of some other algorithm for generation of (pseudo)random numbers.

随机数(和伪随机数)生成器是一个复杂的话题,需要进行一些研究.至少从维基百科开始.

The random (and pseudo random) number generators are complex topic, that need some study. Start at least with wikipedia.

顺便说一句,尽管整个主题很复杂,但是一些随机数生成器非常简单,可以由初学者程序员实现.例如中间方方法.尝试通过将当前种子AX自身相乘并用结果的中间4个十六进制数字形成下一个数字来用汇编语言实现它:

BTW, despite of the complexity of the topic as a whole, some random number generators are simple enough to be implemented by beginner programmers. For example middle-square-method. Try to implement it in assembly language by multiplying the current seed AX by itself and form the next number by the middle 4 hexadecimal digits of the result:

; here ax contains the previous number

    mul ax
    mov al, ah
    mov ah, dl 

; here ax contains the next pseudo random number.

这篇关于在汇编中生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆