0

Here is my code for random number. I want to print random numbers in a loop or to call random numbers many time in a program. How can I get different value for each procedure call? Below program print same value for each call.

.model small
.stack 100
.data
var1 db 0
.code   
main proc
 ; First call
 call random
 mov al,var1
 mov ah,02h
 int 21h
; Second call
 call random
 mov al,var1
 mov ah,02h
 int 21h
;Third call
 call random
 mov al,var1
 mov ah,02h
 int 21h
  mov ah,4ch
 int 21h 
MAIN ENDP
;Random number procedure
random proc
  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, 5 ;Ending    
   div  cx       ; here dx contains the remainder of the division - from 0 to 9

   add  dl, '1'  ; start--to ascii from '0' to '9'
   mov var1,al
   ;mov ah, 2h   ; call interrupt to display a value in DL
   ;int 21h    
  ret
  random endp
END MAIN
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Muhammad
  • 39
  • 5
  • Right off the bat, you are placing the value of AL in var1 instead of DL. Second, you probably should be adding '0' instead of '1' since numbers are zero based. Third, taking the 8-bit product of dividing a 16-bit value by 5 will not give you very many variations. Finally, with Legacy BIOS, a 'tick' is only generated 18.2 times a second. The value returned from the service call will only change 18.2 times a second. Calling this function quite frequently will give you the same value. – fysnet Jun 07 '21 at 23:03
  • 1
    An alternative is to create a random value once, using a 'seed'. Then call a function to retrieve this value, letting the function create a new value before returning. Search for "Topher Cooper and Marc Rieffel". They have a fairly decent RNG. – fysnet Jun 07 '21 at 23:07

0 Answers0