Advertisement
AnthonyCagliano

Untitled

Jan 13th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. rand:
  2. ;;Input: A is the range.
  3. ;;Output: Returns in A a random number from 0 to (input)A-1.
  4. ;; B=0
  5. ;; DE is a pseudo-random 16-bit integer.
  6. ;;Destroys:
  7. ;; HL
  8. ;;Speed:
  9. push af
  10. call prng24
  11. ex de,hl
  12. pop af
  13. or a \ sbc hl,hl
  14. ld b,h
  15. add a,a \ jr nc,$+5 \ push de \ pop hl
  16. add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,b
  17. add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,b
  18. add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,b
  19. add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,b
  20. add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,b
  21. add hl,hl \ rla \ jr nc,$+4 \ add hl,de \ adc a,b
  22. add hl,hl \ rla \ ret nc \ add hl,de \ adc a,b
  23. ret
  24.  
  25. prng24:
  26. ;collab with Runer112
  27. ;;Output:
  28. ;; HL is a pseudo-random int
  29. ;; A and BC are also, but much weaker and smaller cycles
  30. ;; Preserves DE
  31. ;;148cc, super fast
  32. ;;26 bytes
  33. ;;period length: 4,294,901,760
  34. seed1=$+1
  35. ld hl,9999
  36. ld b,h
  37. ld c,l
  38. add hl,hl
  39. add hl,hl
  40. inc l
  41. add hl,bc
  42. ld (seed1),hl
  43. seed2=$+1
  44. ld hl,987
  45. add hl,hl
  46. sbc a,a
  47. and %00101101
  48. xor l
  49. ld l,a
  50. ld (seed2),hl
  51. add hl,bc
  52. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement