Skip to content

wchoice

Weighted random choice selection

Syntax

ichosen wchoice i/kchoices[], i/kweights[]
ichosen = wchoice(i/kchoices[], i/kweights[])

Usage

wchoice picks a random element from an array (kchoices). A second array (kweights) specifies relative probability of selecting the values in kchoices at the same indices as the 'weights' values. If kweights does not match the length of kchoices, kweights is truncated or extended through repetition.


Outputs and arguments

ichosen -- output

i/kchoices -- array of items to select from.

i/kweights -- relative probability of selecting the item in i/kchoices at each index position .

Example

See the example for wchoice.orc

walker
;for pluck
gi_biexp ftgen 0, 0, 16384, 21, 5,1

instr gtr
;; sound source
  ares pluck p4, p5, p5*4, gi_biexp, 2, 4.5

send ares
endin

patchsig "gtr","outs"
;; sound out

instr Sched11

  ipitch wchoice fillarray(-2,-1,-7),fillarray(0.8,0.1,0.1)
  irhythm wchoice fillarray(1/6,1/12,3/6),fillarray(0.7,0.2,0.1)
  ;; generate some rhythms and pitches, with weighted probabilities

  chrdi fillarray_i(n("gtr"), 0, irhythm, 0.5,ipitch), fillarray_i(0,4,5,7,9,13),-3,0,2,0.11
  ;;some slack strumming ---                                                            ^fun

  schedule p1, nextbeat(irhythm), 1
  ;; around again according to the elected rhythm.
turnoff
endin

schedule "Sched11", 0,1
;; start the show