home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / assembler / thesource / volume4 / source / misc / random.lha / Random.txt
Encoding:
Text File  |  1993-03-06  |  3.0 KB  |  87 lines

  1. In article <7631@ucsbcsl.ucsb.edu>,
  2.         bendm@lemon.ucsb.edu (Benjamin W. Berck,,,COMS,Undergraduate) writes:
  3.  
  4.  |-> I've been looking for  some  help  on  making  my  own  random  number
  5.  |-> generator for the Amiga.
  6.  
  7. Here is some source in assembly-language.  This  randomnumber-generator  is
  8. very reliable, but not too fast. For programming games,  I'd  advice  using
  9. the beam-position!
  10.  
  11. ;------------------------------------ cut here ----------------------------
  12. *  You must seed the random function first. It would be best to seed it
  13. *  with the current time, as returned by intuition CurrentTime.
  14. *
  15. *  To generate the number place the limit in d0 and call Random. You are
  16. *  returned a value in the range from 0 to limit-1.
  17. *
  18.  
  19. ;=========================
  20. ; RandomSeed(seed)
  21. ;             d0
  22. ;=========================
  23. RandomSeed:
  24.         ADD.L   D0,D1        ;   user seed in d0 (d1 too)
  25.         MOVEM.L D0/D1,RND
  26. ; drops through to the main random function (not user callable)
  27. LongRnd:
  28.         MOVEM.L D2-D3,-(SP)
  29.         MOVEM.L RND,D0/D1    ;   D0=LSB's, D1=MSB's of random number
  30.         ANDI.B  #$0E,D0      ;   ensure upper 59 bits are an...
  31.         ORI.B   #$20,D0      ;   ...odd binary number
  32.         MOVE.L  D0,D2
  33.         MOVE.L  D1,D3
  34.         ADD.L   D2,D2        ;   accounts for 1 of 17 left shifts
  35.         ADDX.L  D3,D3        ;   [D2/D3] = RND*2
  36.         ADD.L   D2,D0
  37.         ADDX.L  D3,D1        ;   [D0/D1] = RND*3
  38.         SWAP    D3           ;   shift [D2/D3] additional 16 times
  39.         SWAP    D2
  40.         MOVE.W  D2,D3
  41.         CLR.W   D2
  42.         ADD.L   D2,D0        ;   add to [D0/D1]
  43.         ADDX.L  D3,D1
  44.         MOVEM.L D0/D1,RND    ;   save for next time through
  45.         MOVE.L  D1,D0        ;   most random part to D0
  46.         MOVEM.L (SP)+,D2-D3
  47.         RTS
  48.  
  49. ;=========================
  50. ;Random(limit)
  51. ;         d0
  52. ;=========================
  53. Random: MOVE.W  D2,-(SP)
  54.         MOVE.W  D0,D2        ;   save upper limit
  55.         BEQ.S   10$          ;   range of 0 returns 0 always
  56.         BSR.S   LongRnd      ;   get a longword random number
  57.         CLR.W   D0           ;   use upper word (it's most random)
  58.         SWAP    D0
  59.         DIVU.W  D2,D0        ;   divide by range...
  60.         CLR.W   D0           ;   ...and use remainder for the value
  61.         SWAP    D0           ;   result in D0.W
  62. 10$     MOVE.W  (SP)+,D2
  63.         RTS
  64.  
  65. RND     DS.L    2            ;   random number
  66. ;------------------------------------ cut here ----------------------------
  67.  
  68.  
  69.  |-> I don't know how to access the system clock [...]
  70.  
  71. Pretty easy.  :-)  Take a look at Intuition.Library/CurrentTime(),
  72. Timer.Device/TR_GETSYSTIME, Timer.Device/GetSysTime() or  Dos.Library/Date-
  73. Stamp().
  74.  
  75.         bye, Peter
  76.  
  77. --
  78.   Peter Simons - Germany, Usenet: simons@peti.GUN.de, Phone: 0228 / 746061
  79.  
  80.                                       Supporting >Pretty Good Privacy v2.x<
  81. begin 777 PGPv2.1-Public-Key
  82. MF0!- BN$"XL   $" ,K.C1"/&23&5_C_MH8:41%!G8\47TD--#&EPDQ<KR'2X
  83. MNJW+HO\8JREFI+:D9..NJW$& &SBD(YQL[R_R8=ZIF$ !1&T(5!E=&5R(%-IX
  84. 9;6]N<R \<VEM;VYS0'!E=&DN1U5.+F1E/KR_X
  85.  X
  86. end
  87.