home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / rand.lha / Random.s < prev   
Encoding:
Text File  |  1993-01-16  |  1.2 KB  |  57 lines

  1.         moveq    #1,d7
  2.         move.l    #9999,d0
  3. l:        bsr    Random
  4.         dbf    d0,l
  5.         rts
  6.  
  7. ***********************************************************
  8. * Input
  9. *  d7 : seed
  10. *
  11. * Output
  12. *  d7 : pseudo-random number
  13. *
  14. * Uses
  15. *  d2-d7
  16.  
  17. Random:        move.l    d7,d6        ; copy Rand(i)
  18.         move.l    #127773,d2    ; sythetic modulus
  19.         bsr.b    Div        ; divide d6 by 127773
  20.         move.l    d4,d5        ; copy to K1
  21.         muls    #-2836,d5    ; d5 = -2836 * K1
  22.         mulu    #42591,d4    ; multiply d4 by 127773
  23.         move.l    d4,d6
  24.         add.l    d4,d4
  25.         add.l    d6,d4
  26.         sub.l    d4,d7        ; d7 = Rand(i) - K1 * 127773
  27.  
  28.         moveq    #4,d4        ; loop counter
  29. .loop:        move.l    d7,d6        ; multiply d7 by 16807
  30.         lsl.l    #3,d7
  31.         sub.l    d6,d7
  32.         dbf    d4,.loop
  33.  
  34.         add.l    d5,d7        ; d7 = Rand(i + 1)
  35.         bpl.b    .exit
  36.         addi.l    #$7fffffff,d7    ; normalize negative result
  37. .exit:        rts
  38.  
  39. ***********************************************************
  40.  
  41. Div:        add.l    d6,d6        ; shift out unused bit
  42.         moveq    #0,d4        ; quotient
  43.         moveq    #14,d3        ; counter
  44.         move.w    d6,d5        ; save low word of Rand(i)
  45.         swap    d6
  46.         andi.l    #$ffff,d6    ; d6 = Rand(i) DIV 2^15
  47.  
  48. .loop:        add.w    d4,d4        ; line up quotient and..
  49.         add.w    d5,d5        ;   dividend
  50.         addx.l    d6,d6        ; shift in bit of low word
  51.         cmp.l    d2,d6        ; trial subtraction
  52.         bmi.b    .exit
  53.         sub.l    d2,d6        ; real subtraction
  54.         addq.w    #1,d4        ; put 1 in quotient
  55. .exit:        dbf    d3,.loop    ; loop
  56.         rts
  57.