File: generate.fnl

package info (click to toggle)
cloc 2.06-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,064 kB
  • sloc: perl: 30,146; cpp: 1,219; python: 623; ansic: 334; asm: 267; makefile: 244; sh: 186; sql: 144; java: 136; ruby: 111; cs: 104; pascal: 52; lisp: 50; haskell: 35; f90: 35; cobol: 35; objc: 25; php: 22; javascript: 15; fortran: 9; ml: 8; xml: 7; tcl: 2
file content (53 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
; https://github.com/bakpakin/Fennel/raw/master/generate.fnl

;; A general-purpose function for generating random values.

(var generate nil)

(local random-char
       (fn []
         (if (> (math.random) 0.9)
             (string.char (+ 48 (math.random 10)))
             (> (math.random) 0.5)        ; an in-line comment
             (string.char (+ 97 (math.random 26)))
             (> (math.random) 0.5)
             (string.char (+ 65 (math.random 26)))
             (> (math.random) 0.5)
             (string.char (+ 32 (math.random 11)))
             (> (math.random) 0.5)
             (string.char (+ 45 (math.random 2)))
             :else
             (string.char (+ 58 (math.random 5))))))

(local generators {:number (fn [] ; weighted towards mid-range integers
                             (if (> (math.random) 0.9)
                                 (let [x (math.random 2147483647)]
                                   (math.floor (- x (/ x 2))))
                                 (> (math.random) 0.2)
                                 (math.floor (math.random 2048))
                                 :else (math.random)))
                   :string (fn []
                             (var s "")
                             (for [_ 1 (math.random 16)]
                               (set s (.. s (random-char))))
                             s)
                   :table (fn [table-chance]
                            (let [t {}]
                              (var k nil)
                              (for [_ 1 (math.random 16)]
                                ;; no nans plz
                                (set k (generate 0.9))
                                (while (~= k k) (set k (generate 0.9)))
                                (tset t k (generate (* table-chance 1.5))))
                              t))
                   :boolean (fn [] (> (math.random) 0.5))})

(set generate
     (fn [table-chance]
       (local table-chance (or table-chance 0.5))
       (if (> (math.random) 0.5) (generators.number)
           (> (math.random) 0.5) (generators.string)
           (> (math.random) table-chance) (generators.table table-chance)
           :else (generators.boolean))))

generate