Advertisement
Zeda

rand

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