home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / progjour / 1991 / 01 / rand.asm < prev    next >
Assembly Source File  |  1990-10-31  |  807b  |  53 lines

  1.     title    rand - random number
  2.     include    asm.inc
  3.  
  4.     public    rand
  5.     public    random
  6.  
  7.     .data
  8. ; these primes come from Knuth's Seminumerical Algorithms
  9. ; useful prime numbers, table 1, section 4.5.4, page 390
  10. ran_num    dw    0        ; random number seed
  11. ran_mod    dw    0FFEFh        ;  modulus    2^16-17
  12. ran_mul    dw    0FFD9h        ;  multiplier    2^16-39
  13. ran_inc    dw    0FFFFh        ;  increment    -1
  14.  
  15.     .code
  16.  
  17. ;;    rand
  18. ;
  19. ;    exit    AX    random number
  20. ;
  21. rand    proc
  22.     push    dx
  23.     mov    ax,ran_num[bp]
  24.     mul    ran_mul[bp]
  25.     add    ax,ran_inc[bp]
  26.     adc    dx,ZER0
  27.     div    ran_mod[bp]
  28.     xchg    ax,dx
  29.     mov    ran_num[bp],ax
  30.     pop    dx
  31.     ret
  32. rand    endp
  33.  
  34.  
  35. ;;    random
  36. ;
  37. ;    entry    AX    num
  38. ;    exit    AX    random number between 0 and num-1
  39. ;
  40. random    proc
  41.     pushm    cx,dx
  42.     mov    cx,ax
  43.     movx    dx,0
  44.     jcxz    ran1
  45.     call    rand
  46.     div    cx
  47. ran1:    mov    ax,dx
  48.     popm    dx,cx
  49.     ret
  50. random    endp
  51.  
  52.     end
  53.