Skip to content

Instantly share code, notes, and snippets.

@mtnygard
Created February 12, 2017 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtnygard/3e0e5f7d783b0efc127ad5436a5f0441 to your computer and use it in GitHub Desktop.
Save mtnygard/3e0e5f7d783b0efc127ad5436a5f0441 to your computer and use it in GitHub Desktop.
Randomly generate pairs from a table of pairwise weights.
(require [clojure.test.check.generators :as gen])
(defn ptable
[label cols & rows]
(let [ncols (count cols)]
(assert (= 0 (mod (count rows) (inc ncols))))
(mapcat
(fn [[r & vs]]
(map (fn [c v] [v [c r]]) cols vs))
(partition (inc ncols) rows))))
(defn generator
[table]
(gen/frequency
(map #(update % 1 gen/return) table)))
(->
(ptable
:_ [:srcA :srcB :srcC]
:dest1 90 15 5
:dest2 15 40 10
:dest3 15 15 90)
generator
(gen/sample 500))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment