[Function]
(drunk n width {keyword value}* )

Returns a random value constrained to lie between n-widthand n+width according to keyword arguments specified to the function. Calling the function with previous values of n implements random walks (brown noise).

drunk supports the following keyword arguments:

:mode {keyword | number}
Determines what to do if the return value exceeds boundary conditions specified to the function. The following keyword modes are are recognized:
:reflect value is reflected back into bounds
:limit nearest boundary value is returned
:reset value is centered between boundaries
:jump value randomly positioned
:stop returns false as value of function
Otherwise, the value should be the number to return as the value of the function. The default value is :reflect.
:low number
Sets a lower bound on the random walk. If the boundary is exceeded, the value returned is determined by :mode.
:high number
Sets an upper bound on the random walk. If the boundary is exceeded, the value returned is determined by :mode.
:avoid number
If specified, the value returned will not equal number.
:state state
The random state object. The value defaults to *random-state*.

Examples:

Example 1. The drunk function.

(loop repeat 10 
      for r = 60 then (drunk r 3)
      collect r)
 (60 62 61 60 58 60 62 63 65 67)
(drunk 40 2 :low 60 :high 72)
 63
(drunk 40 2 :low 60 :high 72 :mode ':stop)
 #f

Example 2. Random walk for keynum and amplitude.

(define (stagger len low high step)
  (let ((k (/ (- high low) 2))
        (a (between .1 .9)))
    (process repeat len
             for r = (pick 1/8 1/4) 
             set k = (drunk k step :high high
                            :low low :mode ':jump)
             set a = (drunk a .1 :low .2 
                            :high .9 :mode .5)
             output (new midi :time (now)
                         :duration (* r 1.5)
                         :keynum k
                         :amplitude a)
             wait r)))

(events (list (stagger 80 40 80 3) 
              (stagger 60 20 40 7)
              (stagger 40 70 90 5))
       "test.mid" '(0 4 6))
 "test.mid"

See also: