Welcome to the CHICKEN Scheme pasting service

1d random walk added by anonymous on Thu Oct 19 22:02:23 2017

(use traversal)
(use int-limits)
(use srfi-1)
;;;;;;;;;;;;;;;;;;;;;;;;;
;misc
(define (normalize val max)
  (/ val max)
  )

(define (scanl f init ls)
  (cons init
        (if (null? ls)
            '()
            (scanl f (f init (first ls)) (rest ls)))))
;;;;;;;;;;;;;;;;;;;;;;;;;
;rng stuff
(define rng_inited #f)
(define rng_seed 1)
(define (init-rng)
  (randomize rng_seed)
  (set! rng_inited #t)
  )

(define (norm-rng) 
  (if (not rng_inited)
      (init-rng))
  (let ((maxint most-positive-integer32))
    (normalize (random maxint) maxint)))

(define (genrn cnt)
  (list-tabulate cnt
    (lambda (i) (norm-rng))))

;;;;;;;;;;;;;;;;;;;;;;;;;
;random sampling processing

(define (p-action-idx rand interval-sizes)
  (define (p-action-index_ interval-sizes idx acc)
    (define fst (first interval-sizes))
    (if (negative? (- acc fst)) 
        idx
        (p-action-index_ (rest interval-sizes) (+ 1 idx) (- acc fst))))
  
  (assert (= (sum interval-sizes) 1) )
  (p-action-index_ interval-sizes 0 rand))

;;;;;;;;;;;;;;;;;;;;;;;;;
;simulation procs

(define (choose-step choice)
  (assert (and (<= 0 choice) (<= choice 1)))
  (case choice
    ((0) -1)
    ((1) 1) ))
;;;;;;;;;;;;;;;;;;;;;;;;;
(define N 10)

(define (main)
  (define rands (genrn N))

  (let* ((init-pos 0)
         (p-interval-sizes '(0.5 0.5))
         (direction (lambda (rand) (choose-step (p-action-idx rand p-interval-sizes))))
         (steps (map direction rands)) )
    (scanl + init-pos steps))
  )
  
(print (main))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Which module provides `foreign-declare'?
Visually impaired? Let me spell it for you (wav file) download WAV