-
Notifications
You must be signed in to change notification settings - Fork 1
/
random.clj
48 lines (38 loc) · 1.31 KB
/
random.clj
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
;; Utility functions for generating random data of a given type.
(ns random.core)
(defn uuid
"Generate a random UUID string, 32 hexadecimal digits separated by dashes."
[]
(str (java.util.UUID/randomUUID)))
(defn id
"Generate a random system identifier, typically a 4–5 digit number."
[]
(rand-int 10000))
(defn ip
"Generate a random IPv4 address, without regard to reserved IPs."
[]
(clojure.string/join "." (repeatedly 4 #(rand-int 256))))
(def ^:private alpha-num "abcdefghijklmnopqrstuvwxyz0123456789")
(defn- rand-char
"Generate a random, lowercase alpha-numeric character."
[]
(.charAt alpha-num (rand-int 36)))
(defn url
"Generate a random, vaguely plausible URL in the form \"https://prefix.company.com\"."
[]
(let [prefix (apply str (repeatedly (+ 2 (rand-int 8)) rand-char))
company (apply str (repeatedly (+ 4 (rand-int 25)) rand-char))]
(apply str "https://" prefix "." company ".com")))
(defn bool
"Generate a random boolean."
[]
(if (zero? (rand-int 2)) false true))
(def ^:private hex-num "0123456789abcdef")
(defn- rand-hex
"Generate a random hexidecimal digit as a character."
[]
(.charAt hex-num (rand-int 16)))
(defn hex-str
"Generate a random hexidecimal string 24 characters long, good for simulating a cookie."
[]
(apply str (repeatedly 24 rand-hex)))