Random numbers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Random numbers

How do I generate random numbers in Assembly?


Comments

  • randomprocedure proc

    push DX

    push DS

    ;============connect randomness to time and number sequence is not as repetitive

    mov AH, 2Ch ; get time function

    int 21h

    .IF (DL > 50)

    mov AH, 0

    mov AL, DH

    mov CL, DL

    mul CL

    inc AX

    add random1, AX

    .ENDIF

    ;=================================================================

    mov AX, DataSeg ; name of data segment

    mov DS, AX

    mov AX, random1 ; DW random word like 345

    mul random2

    inc AX

    mov random1, AX ; DW random word can be any number like 999

    pop DS

    pop DX

    ret

    randomprocedure endp





  • ; here is a complete random number program calling the above procedure

    TITLE random number program

    ;........................................................................

    msg MACRO msgout

    mov AH, 09h ; function 09h

    lea DX, msgout ; DX holds message

    int 21h ; display string interupt

    ENDM

    ;........................................................................

    .MODEL small ; size

    ;........................................................................

    ;

    ;...................................; stack segment......................

    StackSeg segment stack ; stack segment begins

    DB 2000 DUP('Q') ; amass 2000 bytes of memory

    StackSeg ends ; stack segment ends

    ;........................................................................

    ;

    ;...................................; data segment.......................

    DataSeg segment


    doit DB 0Ah,0Dh,0Ah,0Dh," If you want more random numbers input r or R!",0Ah,0Dh,0Ah,0Dh," ",'$'

    Message DB 0Ah,0Dh,0Ah,0Dh," This program generates random numbers between 0 to 1000!",0Ah,0Dh,0Ah,0Dh,'$'

    CW DB 0Ah,0Dh,0Ah,0Dh," RANDOM NUMBER PROGRAM IS TERMINATED!",0AH,0Dh," (C)1998 X COMPANY",0Ah,0Dh,'$'

    nl DB 0Ah,0Dh," ",'$'

    random1 DW 884

    random2 DW 9999

    counter DB 0


    DataSeg ends ; data segment ends

    ;........................................................................

    ;

    ;...................................; code segment begins................

    CodeSeg segment

    ;........................................................................

    ;

    ;.............................; tell assembler which segment is in what register

    assume cs:CodeSeg, ds:DataSeg, ss:StackSeg

    ;........................................................................

    ;

    start: ; begin algorithm

    ;........................................................................

    ; ; move data segment into DS

    ;........................................................................

    mov AX,DataSeg ; base address

    mov DS,AX ; of data segment

    ;........................................................................

    msg Message

    ;........................................................................

    rloop:

    mov AH, 2Ch ; get time function

    int 21h

    callloop:

    .IF (DL < 200) ; DL = hundredths of seconds

    inc DL

    call randomprocedure

    jmp callloop

    .ENDIF

    ;........................................................................

    lessthanoneloop:

    .IF (random1 > 1000)

    mov AX, random1

    sub AX, 1000

    shr AX, 1

    mov random1, AX

    jmp lessthanoneloop

    .ENDIF

    ;........................................................................

    ; ; output random number

    ;........................................................................

    push ax ; values on stack

    push bx ; values on stack

    push cx ; values on stack

    push dx ; values on stack

    msg nl

    mov AX, random1 ; place number to be printed into AX.

    mov bx,10 ; place number '10'(divider) into DI.

    mov cx,0 ;

    rDiv: ;

    mov dx,0 ;

    div bx ; divide command. After this result is in

    push dx ; push Remainder into stack.

    inc cx ; increase counter

    cmp ax,0 ; check if result is zero and

    jne rDiv ;

    mov ax,200h ; display output function

    rSym: pop dx ;

    add dl,'0' ;

    int 21h ; call interrupt

    loop rSym ;

    pop dx ; values off stack

    pop cx ; values off stack

    pop bx ; values off stack

    pop ax ; values off stack

    ;........................................................................

    inc counter

    .IF (counter < 10)

    jmp rloop

    .ENDIF

    ;........................................................................

    msg doit ; do you want to do it again

    mov AH, 01h ; read character function

    int 21h ; call interrupt

    .IF(AL == 82 || AL == 114) ; if 'R' of 'r' entered

    mov counter, 0

    jmp rloop

    .ENDIF

    msg CW

    ;........................................................................

    mov AX,4c00h ;

    int 21h ; termination interupt

    ;........................................................................

    ; ; random number prdcedure

    ;........................................................................

    randomprocedure proc

    push DX

    push DS

    ;============connect randomness to time and number sequence is much less repetitive

    mov AH, 2Ch ; get time function

    int 21h

    .IF (DL > 50)

    mov AH, 0

    mov AL, DH

    mov CL, DL

    mul CL

    inc AX

    add random1, AX

    .ENDIF

    ;=================================================================

    mov AX, DataSeg ; name of data segment

    mov DS, AX

    mov AX, random1 ; DW random word like 345

    mul random2

    inc AX

    mov random1, AX ; DW random word can be any number like 999

    pop DS

    pop DX

    ret

    randomprocedure endp

    ;........................................................................

    CodeSeg ends ; end algorithm

    ;........................................................................

    end start ; end program


  • Thank you very much.




Sign In or Register to comment.