[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/RPq4Ocqx    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Mar 3:
; chutes and ladders

(define rand #f)
(define randint #f)
(let ((two31 #x80000000) (a (make-vector 56 -1)) (fptr #f))
  (define (mod-diff x y) (modulo (- x y) two31)) ; generic version
  ; (define (mod-diff x y) (logand (- x y) #x7FFFFFFF)) ; fast version
  (define (flip-cycle)
    (do ((ii 1 (+ ii 1)) (jj 32 (+ jj 1))) ((< 55 jj))
      (vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
    (do ((ii 25 (+ ii 1)) (jj 1 (+ jj 1))) ((< 55 ii))
      (vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
    (set! fptr 54) (vector-ref a 55))
  (define (init-rand seed)
    (let* ((seed (mod-diff seed 0)) (prev seed) (next 1))
      (vector-set! a 55 prev)
      (do ((i 21 (modulo (+ i 21) 55))) ((zero? i))
        (vector-set! a i next) (set! next (mod-diff prev next))
        (set! seed (+ (quotient seed 2) (if (odd? seed) #x40000000 0)))
        (set! next (mod-diff next seed)) (set! prev (vector-ref a i)))
      (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle)))
  (define (next-rand)
    (if (negative? (vector-ref a fptr)) (flip-cycle)
      (let ((next (vector-ref a fptr))) (set! fptr (- fptr 1)) next)))
  (define (unif-rand m)
    (let ((t (- two31 (modulo two31 m))))
      (let loop ((r (next-rand)))
        (if (<= t r) (loop (next-rand)) (modulo r m)))))
  (init-rand 19380110) ; happy birthday donald e knuth
  (set! rand (lambda seed
    (cond ((null? seed) (/ (next-rand) two31))
          ((eq? (car seed) 'get) (cons fptr (vector->list a)))
          ((eq? (car seed) 'set) (set! fptr (caadr seed))
                                 (set! a (list->vector (cdadr seed))))
          (else (/ (init-rand (modulo (numerator
                  (inexact->exact (car seed))) two31)) two31)))))
  (set! randint (lambda args
    (cond ((null? (cdr args))
            (if (< (car args) two31) (unif-rand (car args))
              (floor (* (next-rand) (car args)))))
          ((< (car args) (cadr args))
            (let ((span (- (cadr args) (car args))))
              (+ (car args)
                 (if (< span two31) (unif-rand span)
                   (floor (* (next-rand) span))))))
          (else (let ((span (- (car args) (cadr args))))
                  (- (car args)
                     (if (< span two31) (unif-rand span)
                       (floor (* (next-rand) span))))))))))

(define chutes '((16 . 6) (47 . 26) (49 . 11)
        (56 . 53) (62 . 19) (64 . 60) (87 . 24)
        (93 . 73) (95 . 75) (98 . 78)))

(define ladders '((1 . 38) (4 . 14) (9 . 31)
        (21 . 42) (28 . 84) (36 . 44) (51 . 67)
        (71 . 91) (80 . 100)))
            
(define (game)
  (let loop ((ps '(0)))
    (let* ((die (randint 6 0)) (p (+ (car ps) die)))
      (cond ((= 100 (car ps)) (cdr (reverse ps)))
            ((< 100 p) (loop (cons (car ps) ps)))
            ((or (assoc p chutes) (assoc p ladders))
              => (lambda (x) (loop (cons (cdr x) ps))))
            (else (loop (cons p ps)))))))

(define (games n)
  (let loop ((n n) (gs '()))
    (if (zero? n) gs
      (loop (- n 1) (cons (length (game)) gs)))))

(define (compete k n)
  (let loop ((n n) (gs '()))
    (if (zero? n) gs
      (loop (- n 1) (cons (apply min (games k)) gs)))))

(define (stats k n)
  (let ((gs (compete k n)))
    (values (apply min gs) (apply max gs)
      (exact->inexact (/ (apply + gs) n)))))

(call-with-values
  (lambda () (stats 1 5000))
  (lambda (mn mx av)
    (display mn) (display " ")
    (display mx) (display " ")
    (display av) (newline)))

(call-with-values
  (lambda () (stats 2 2000))
  (lambda (mn mx av)
    (display mn) (display " ")
    (display mx) (display " ")
    (display av) (newline)))

(call-with-values
  (lambda () (stats 3 2000))
  (lambda (mn mx av)
    (display mn) (display " ")
    (display mx) (display " ")
    (display av) (newline)))

(call-with-values
  (lambda () (stats 4 2000))
  (lambda (mn mx av)
    (display mn) (display " ")
    (display mx) (display " ")
    (display av) (newline)))

(call-with-values
  (lambda () (stats 5 1000))
  (lambda (mn mx av)
    (display mn) (display " ")
    (display mx) (display " ")
    (display av) (newline)))


Output:
1
2
3
4
5
7 263 39.2846
7 93 26.4075
7 91 21.8435
7 67 19.4525
7 65 17.678


Create a new paste based on this one


Comments: