Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created March 30, 2014 08:43
Show Gist options
  • Save kurogelee/9869725 to your computer and use it in GitHub Desktop.
Save kurogelee/9869725 to your computer and use it in GitHub Desktop.
Clojureでランダムな文字列を生成してみる ref: http://qiita.com/kurogelee/items/e740d6251f87fb87b29b
user=> (range-size 50 5)
(50 51 52 53 54)
user=> (rand-str 20)
"195Tbl1nE8ME0QGEwJl7"
user=> (rand-str 20 "abc")
"cbabbbcabbbaaaabcaca"
(defn range-size
([start size] (range-size start size 1))
([start size step]
{:pre[(integer? size)]}
(take size (range start (+ start (* size step)) step))))
(defn rand-str
([n] (rand-str n (mapcat #(apply range-size %) [[48 10] [65 26] [97 26]])))
([n charseq] (apply str (map char (repeatedly n #(rand-nth charseq))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment