Created
March 30, 2014 08:43
-
-
Save kurogelee/9869725 to your computer and use it in GitHub Desktop.
Clojureでランダムな文字列を生成してみる ref: http://qiita.com/kurogelee/items/e740d6251f87fb87b29b
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
user=> (range-size 50 5) | |
(50 51 52 53 54) | |
user=> (rand-str 20) | |
"195Tbl1nE8ME0QGEwJl7" | |
user=> (rand-str 20 "abc") | |
"cbabbbcabbbaaaabcaca" |
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
(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