First of all, I am very new to the 8086 Assembly, and it was quite difficult for me to grab knowledge. However, I will do my best.
I am trying to write code to generate a random number in the range from 0 to 9. Having studied several examples and suggestions, this is what I came across. For simplicity, I did not use the mathematical function of the resulting count of hours, and also thought that this was not necessary. For some reason, I got a certain number, for example, 6.7 times less than numbers, such as 1.3 and 9. I believe that this is because I take a lower order of measures when the values ββchange so quickly.
My goal is to simulate a roll of the bone, which later changes the range below codes by 1-6 poorly. My question is: is this enough for my purpose? or is there a better way to do this?
codes:
RANDGEN: ; generate a rand no using the system time RANDSTART: MOV AH, 00h ; interrupts to get system time INT 1AH ; CX:DX now hold number of clock ticks since midnight ; lets just take the lower bits of DL for a start.. MOV BH, 57 ; set limit to 57 (ASCII for 9) MOV AH, DL CMP AH, BH ; compare with value in DL, JA RANDSTART ; if more, regenerate. if not, continue... MOV BH, 49 ; set limit to 48 (ASCII FOR 0) MOV AH, DL CMP AH, BH ; compare with value in DL JB RANDSTART ; if less, regenerate. ; if not, this is what we need mov ah, 2h ; call interrupt to display a value in DL int 21h RET
Answer, by @johnfound:
I found that its path is easier, and it takes less time to generate a random number. He mentioned that this only works if you need one random number, or the interval between random numbers includes pauses for human input. If not, the numbers will not be random at all (I believe, because of the time that we initially accept, it does not change). This is normal for my case, as I simulate a roll die and I need user intervention (another roll) before I run the code again.
RANDGEN: ; generate a rand no using the system time RANDSTART: MOV AH, 00h ; interrupts to get system time INT 1AH ; CX:DX now hold number of clock ticks since midnight mov ax, dx xor dx, dx mov cx, 10 div cx ; here dx contains the remainder of the division - from 0 to 9 add dl, '0' ; to ascii from '0' to '9' mov ah, 2h ; call interrupt to display a value in DL int 21h RET
What he did: 1. We moved the value in DX to AX 2. We cleared DX. 3. We have moved 10 decites to CX. 4. We split AX into CX, so we get a balance of 0-9 decites stored in DX 5. Finally, we added ASCII '0' (dec 48) to DX to get them in ASCII '0' to '9 '.