| 1 |
; https://github.com/bakpakin/Fennel/raw/master/generate.fnl |
| 2 |
|
| 3 |
;; A general-purpose function for generating random values. |
| 4 |
|
| 5 |
(var generate nil) |
| 6 |
|
| 7 |
(local random-char |
| 8 |
(fn [] |
| 9 |
(if (> (math.random) 0.9) |
| 10 |
(string.char (+ 48 (math.random 10))) |
| 11 |
(> (math.random) 0.5) ; an in-line comment |
| 12 |
(string.char (+ 97 (math.random 26))) |
| 13 |
(> (math.random) 0.5) |
| 14 |
(string.char (+ 65 (math.random 26))) |
| 15 |
(> (math.random) 0.5) |
| 16 |
(string.char (+ 32 (math.random 11))) |
| 17 |
(> (math.random) 0.5) |
| 18 |
(string.char (+ 45 (math.random 2))) |
| 19 |
:else |
| 20 |
(string.char (+ 58 (math.random 5)))))) |
| 21 |
|
| 22 |
(local generators {:number (fn [] ; weighted towards mid-range integers |
| 23 |
(if (> (math.random) 0.9) |
| 24 |
(let [x (math.random 2147483647)] |
| 25 |
(math.floor (- x (/ x 2)))) |
| 26 |
(> (math.random) 0.2) |
| 27 |
(math.floor (math.random 2048)) |
| 28 |
:else (math.random))) |
| 29 |
:string (fn [] |
| 30 |
(var s "") |
| 31 |
(for [_ 1 (math.random 16)] |
| 32 |
(set s (.. s (random-char)))) |
| 33 |
s) |
| 34 |
:table (fn [table-chance] |
| 35 |
(let [t {}] |
| 36 |
(var k nil) |
| 37 |
(for [_ 1 (math.random 16)] |
| 38 |
;; no nans plz |
| 39 |
(set k (generate 0.9)) |
| 40 |
(while (~= k k) (set k (generate 0.9))) |
| 41 |
(tset t k (generate (* table-chance 1.5)))) |
| 42 |
t)) |
| 43 |
:boolean (fn [] (> (math.random) 0.5))}) |
| 44 |
|
| 45 |
(set generate |
| 46 |
(fn [table-chance] |
| 47 |
(local table-chance (or table-chance 0.5)) |
| 48 |
(if (> (math.random) 0.5) (generators.number) |
| 49 |
(> (math.random) 0.5) (generators.string) |
| 50 |
(> (math.random) table-chance) (generators.table table-chance) |
| 51 |
:else (generators.boolean)))) |
| 52 |
|
| 53 |
generate |