home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / 80x0393.zip / RAND.ASM < prev    next >
Assembly Source File  |  1993-03-30  |  3KB  |  79 lines

  1. comment |
  2.  
  3.   rand.asm
  4.  
  5. public domain, linear congruential pseudo-random
  6. number generator routine by Jerry Coffin.
  7.  
  8. These do have a bit of a defect - there's one bit that actually
  9. is predictable - the two rotates at the end of rand bury it in
  10. the middle of the number instead of the end where it causes
  11. numbers to alternate between odd and even.  As is, in binary you
  12. can still see the alternation, but when converted to decimal, it
  13. generally isn't too obvious.  An alternative is to use a 32 bit
  14. generator and only use 16 bits of what it produces.  With 16 bit
  15. registers, this is quite a bit more work, and for light duty use,
  16. this one seems to be adequate. For serious statistical analysis,
  17. you should probably investigate other algorithms.
  18.  
  19. srand should be called before rand is used, but needs only to be
  20. called once at the beginning of the program.  It uses the seconds
  21. and hundredths of a second read from DOS to seed the generator
  22. which is relatively random due to the imprecision of the clock,
  23. but as noted above, it's likely not particularly good for serious
  24. statistical analysis but seems to work fine for games and such.
  25.  
  26. Assemble with a definition to tell it what memory model to use, as in:
  27.                                                                       
  28. tasm /Dmemmodel=small /mx rand                                        
  29.                                                                       
  30. As is, these will assemble to produce names that a c compiler         
  31. will recognize (lower case with an underscore before the              
  32. function name.) If you wish to call from BASIC, Pascal, etc.          
  33. change the language on the model line to suit...                      
  34.  
  35. |
  36.  
  37. %.model memmodel,c
  38.  
  39. .data
  40. k1      dw      9821
  41. k2      dw      1
  42. ;
  43. ; These two values are relatively critical to the quality of
  44. ; number generated.  They were picked in accordance with Knuth
  45. ; Vol. 2, which you should likely read youself if you want to
  46. ; modify this or produce another of your own...
  47. ;
  48. seed    dw      ?
  49.  
  50. .code
  51.  
  52.         public rand
  53. rand proc
  54. ;
  55. ; returns a 16 bit semi-poor quality pseudo-random number in AX.
  56. ;
  57. ; destroys AX and DX.
  58. ;
  59.         mov     ax,seed
  60.         imul    k1
  61.         add     ax,k2
  62.         mov     seed,ax
  63.         ror     al,1
  64.         rol     ah,1
  65.         ret
  66. rand endp
  67.  
  68.         public srand
  69. srand proc
  70. ;
  71. ; Seeds rand from the DOS clock.
  72. ;
  73.         mov     ah,2ch
  74.         int     21h
  75.         mov     seed,dx
  76.         ret
  77. srand   endp
  78.         end
  79.