home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / ASM / ALIB30B / RANDOM6.ASM < prev    next >
Assembly Source File  |  1994-11-14  |  2KB  |  82 lines

  1.     page    66,132
  2. ;******************************* RANDOM6.ASM *********************************
  3.  
  4. LIBSEG           segment byte public "LIB"
  5.         assume cs:LIBSEG , ds:nothing
  6.  
  7. ;----------------------------------------------------------------------------
  8. .xlist
  9.     include  mac.inc
  10.     include  common.inc
  11.     extrn    random_seed:far
  12. .list
  13.  
  14. init_flag6    db    0
  15.  
  16. comment 
  17. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -( RANDOM  )
  18. RANDOM_WORD4 - generate random word value, method 4
  19.    inputs:  none
  20.    output:  ax = random value
  21. * * * * * * * * * * * * * *
  22. 
  23.  
  24. old_rand    dw    123,45        ;
  25. permutter    dw    0DE6DH,0278Dh    ;
  26.  
  27.  
  28.     public        RANDOM_WORD4
  29. RANDOM_WORD4    proc    far
  30.         apush    bx,cx,dx
  31.         cmp    cs:init_flag6,0
  32.         jne    cont6        ;jmp if initialized
  33.         call    rand_init
  34. cont6:        mov    bx,permutter
  35.         mov    cx,bx        
  36.         mov    ax,permutter+2    
  37.         mul    old_rand    
  38.         xchg    ax,bx        
  39.         mul    old_rand    
  40.         add    bx,dx        
  41.         xchg    ax,cx        
  42.         mul    old_rand+2    
  43.         add    ax,bx        
  44.         mov    old_rand,cx    
  45.         mov    old_rand+2,ax    ;save random to permute next rand
  46.         apop    dx,cx,bx
  47.         retf
  48. RANDOM_WORD4    endp
  49.  
  50. ;---------------------------------
  51. rand_init:    apush    ax,dx
  52.         call    random_seed
  53.         or    al,1        ;make sure the dword seed is odd
  54.         mov    old_rand,ax    ;store the (odd) low word
  55.         mov    old_rand+2,dx    ;store high word of seed
  56.         apop    ax,dx
  57.         ret
  58.  
  59. comment 
  60. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -( RANDOM  )
  61. RANDOM_WORD4_SCALED - Generates a random number within a specific range
  62.   inputs:  ax =    max value +1 of random number desired
  63.   output:  ax = random value
  64. * * * * * * * * * * * * * *
  65. 
  66.     public        RANDOM_WORD4_SCALED
  67. RANDOM_WORD4_SCALED    proc    far
  68.         apush    dx,ax
  69.         call    RANDOM_WORD4    ;get a random number
  70.         pop    bx
  71.         test    bx,bx        ;return within restricted range?
  72.         jz    scale_end     ;  n: return 16-bit random number
  73.         xor    dx,dx        ;convert to dword for divide
  74.         div    bx        ;divide by the limit
  75.         xchg    ax,dx        ;return the remainder in AX
  76. scale_end:    pop    dx
  77.         retf
  78. RANDOM_WORD4_SCALED    endp
  79.  
  80. LIBSEG    ENDS
  81. ;;    end
  82.