diff --git a/src/hidden_markov_music/random.clj b/src/hidden_markov_music/random.clj new file mode 100644 index 0000000..07570d4 --- /dev/null +++ b/src/hidden_markov_music/random.clj @@ -0,0 +1,21 @@ +(ns hidden-markov-music.random) + +(defn rand* + "Returns a random floating point number between 0 (exclusive) and + n (default 1) (inclusive)." + ([] + (rand* 1.0)) + ([n] + (- n (rand n)))) + +(defn select-random-key + "Takes a collection of `[key prob]` pairs, whose `prob`s sum to 1.0, and + selects a `key` randomly based on its `prob`." + [key->prob] + (loop [key->prob key->prob + selection-index (rand*)] + (let [[k p] (first key->prob) + new-selection-index (- selection-index p)] + (if (pos? new-selection-index) + (recur (rest key->prob) new-selection-index) + k))))