Created
February 12, 2017 04:14
-
-
Save mtnygard/3e0e5f7d783b0efc127ad5436a5f0441 to your computer and use it in GitHub Desktop.
Randomly generate pairs from a table of pairwise weights.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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