home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol022 / random.mac < prev    next >
Encoding:
Text File  |  1984-04-29  |  3.1 KB  |  87 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;
  3. ;        PSUEDO RANDOM NO.GENERATOR
  4. ;         by Bob Harsch
  5. ;            2362 American River Dr
  6. ;            Suite 311
  7. ;            Sacramento, 95825
  8. ;
  9. ;  This is a professional program that is fully copy
  10. ;  righted and is not to be used, copyed or stored in
  11. ;  any form whatsoever. Bob Harsch is donating this 
  12. ;  program to the Pascal/Z Users Group for the personal
  13. ;  use of its members only.  No commercial use allowed 
  14. ;  at all.  When used in a members program, credit must 
  15. ;  be given to Bob Harsch.  This approach has been under
  16. ;  test for two years so a lot of time and effort has 
  17. ;  gone into proving this program.
  18. ;  This is the best Random number generator I have seen
  19. ;  so give Bob credit due him.  You may contact him 
  20. ;  directly, if you desire, for applications other
  21. ;  than personal use.
  22. ;
  23. ;    Published by the Pascal/Z Users Group, Aug 1980
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25.  
  26. .COMMENT }
  27. { Function to generate a random byte for a calling PASCAL/Z
  28. program.  Result returned in DE where D = 0 and E = random
  29. byte.  External header declaration for calling sequence
  30. should be:
  31.  
  32. type    byte = 0..255;
  33.     seedarray = array [ 0..n ] of byte;
  34. function ranbyt( var seeds : seedarray ) : byte; external;
  35.  
  36.     Where the range of n is 5 <= n <= 255  and seeds[0] 
  37. equals the number of seeds that will be used iteratively to 
  38. create a random byte from seeds[1] on.  The range of 
  39. seeds[0] should be: 5 <= seeds[0] <= n, but this routine is
  40. fastest when seeds[0] = 5.
  41.      By appending the random bytes together, formed from 
  42. consecutive calls, a large random number may be obtained.  
  43. The array seeds is returned altered ( cell values seeds[1] 
  44. to seeds[ seeds[0] ]) for future random byte generations.
  45.     This routine may be used to hash a variable length 
  46. string into a direct address by subsituting the symbol 
  47. string itself as the seeds values ( padding with blanks to
  48. produce the needed length of 5 or greater).
  49. }
  50.     .Z80
  51.     ENTRY    RANBYT
  52. RANBYT:    POP    DE    ;RETURN ADDRESS.
  53.     POP    HL    ;TOP ADDRESS OF ARRAY SEEDS.
  54.     LD    A,(HL)    ;A= NUMBER OF SEEDS IN ARRAY.
  55.     LD    B,A    ;B= NUMBER OF SEEDS IN ARRAY.
  56.     CP    5    ;SET FLAGS, TEST IF SEED NUMBER < 5.
  57.     LD    A,0    ;A=0 IN CASE OF ERROR. FLAGS UNCHANGED.
  58.     JR    C,ERROR    ;0 THRU 4 SEEDS ARE WRONG,RET ZERO.
  59.     DEC    B
  60.     DEC    B    ;LAST TWO CELLS IN ARRAY IS OPERATED
  61.             ; ON OUTSIDE THE LOOP.
  62.     DEC    HL    ;(HL)= FIRST SEED IN ARRAY.
  63. ;
  64. ;ITERATE SEEDS[0]-2 TIMES TO COMPUTE RANDOM BYTE IN A REG.
  65. LOOP:    INC    (HL)    ;INCREMENT SEED VALUE.
  66.     LD    A,(HL)
  67.     DEC    HL    ;HL POINTS TO NEXT SEED VALUE IN ARRAY.
  68.     ADD    A,(HL)
  69.     RRCA        ;ROTATE RIGHT CIRCULAR ACCUMULATOR.
  70.     LD    (HL),A
  71.     DJNZ    LOOP
  72. ;
  73. ;LAST ITERATION TO COMPUTE RANDOM BYTE IN A REG.
  74.     DEC    HL    ;HL POINTS TO LAST SEED VALUE IN ARRAY.
  75.     ADD    A,(HL)
  76.     CPL        ;INSTR NOT IN LOOP. COMPLEMENT ACCUM.
  77.     RRCA        ;ROTATE RIGHT CIRCULAR ACCUMULATOR.
  78.     LD    (HL),A
  79. ;
  80. ;ASSIGN DE TO RANDOM BYTE IN A REG. BEFORE RETURNING.
  81. ERROR:    EX    DE,HL    ;HL= RETURN ADDR, DE= RANDOM BYTE.
  82.     LD    D,0
  83.     LD    E,A    ;PASCAL/Z FUNCTION RETURN CONVENTION.
  84.     XOR    A    ;A=0 FOR PASCAL/Z.
  85.     JP    (HL)    ;RETURN.
  86.     END
  87.