fork download
  1. ; the hot hand
  2.  
  3. (define rand #f)
  4. (define randint #f)
  5. (let ((two31 #x80000000) (a (make-vector 56 -1)) (fptr #f))
  6. (define (mod-diff x y) (modulo (- x y) two31)) ; generic version
  7. ; (define (mod-diff x y) (logand (- x y) #x7FFFFFFF)) ; fast version
  8. (define (flip-cycle)
  9. (do ((ii 1 (+ ii 1)) (jj 32 (+ jj 1))) ((< 55 jj))
  10. (vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
  11. (do ((ii 25 (+ ii 1)) (jj 1 (+ jj 1))) ((< 55 ii))
  12. (vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
  13. (set! fptr 54) (vector-ref a 55))
  14. (define (init-rand seed)
  15. (let* ((seed (mod-diff seed 0)) (prev seed) (next 1))
  16. (vector-set! a 55 prev)
  17. (do ((i 21 (modulo (+ i 21) 55))) ((zero? i))
  18. (vector-set! a i next) (set! next (mod-diff prev next))
  19. (set! seed (+ (quotient seed 2) (if (odd? seed) #x40000000 0)))
  20. (set! next (mod-diff next seed)) (set! prev (vector-ref a i)))
  21. (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle)))
  22. (define (next-rand)
  23. (if (negative? (vector-ref a fptr)) (flip-cycle)
  24. (let ((next (vector-ref a fptr))) (set! fptr (- fptr 1)) next)))
  25. (define (unif-rand m)
  26. (let ((t (- two31 (modulo two31 m))))
  27. (let loop ((r (next-rand)))
  28. (if (<= t r) (loop (next-rand)) (modulo r m)))))
  29. (init-rand 19380110) ; happy birthday donald e knuth
  30. (set! rand (lambda seed
  31. (cond ((null? seed) (/ (next-rand) two31))
  32. ((eq? (car seed) 'get) (cons fptr (vector->list a)))
  33. ((eq? (car seed) 'set) (set! fptr (caadr seed))
  34. (set! a (list->vector (cdadr seed))))
  35. (else (/ (init-rand (modulo (numerator
  36. (inexact->exact (car seed))) two31)) two31)))))
  37. (set! randint (lambda args
  38. (cond ((null? (cdr args))
  39. (if (< (car args) two31) (unif-rand (car args))
  40. (floor (* (next-rand) (car args)))))
  41. ((< (car args) (cadr args))
  42. (let ((span (- (cadr args) (car args))))
  43. (+ (car args)
  44. (if (< span two31) (unif-rand span)
  45. (floor (* (next-rand) span))))))
  46. (else (let ((span (- (car args) (cadr args))))
  47. (- (car args)
  48. (if (< span two31) (unif-rand span)
  49. (floor (* (next-rand) span))))))))))
  50.  
  51. (define (hot-hand n)
  52. (let ((streak 0) (break 0))
  53. (do ((i 0 (+ i 1)))
  54. ((= n i) (values streak break))
  55. (let ((one (randint 2))
  56. (two (randint 2))
  57. (three (randint 2))
  58. (four (randint 2)))
  59. (when (and (= one 1) (= two 1)) (set! streak (+ streak 1)))
  60. (when (and (= one 1) (= two 0)) (set! break (+ break 1)))
  61. (when (and (= two 1) (= three 1)) (set! streak (+ streak 1)))
  62. (when (and (= two 1) (= three 0)) (set! break (+ break 1)))
  63. (when (and (= three 1) (= four 1)) (set! streak (+ streak 1)))
  64. (when (and (= three 1) (= four 0)) (set! break (+ break 1)))))))
  65.  
  66. (call-with-values
  67. (lambda () (hot-hand 10000))
  68. (lambda (streak break)
  69. (display streak) (newline)
  70. (display break) (newline)))
Success #stdin #stdout 0.61s 7368KB
stdin
Standard input is empty
stdout
7442
7453