Skip to content

Instantly share code, notes, and snippets.

@minoki
Last active April 7, 2016 17:31
Show Gist options
  • Save minoki/617963e05e21cf2120f9f64fe522eea9 to your computer and use it in GitHub Desktop.
Save minoki/617963e05e21cf2120f9f64fe522eea9 to your computer and use it in GitHub Desktop.
ランダムなひらがな文字列を生成する。opensslコマンド使用。
local chars = (([[
あいうえお
かきくけこ
さしすせそ
たちつてと
なにぬねの
はひふへほ
まみむめも
やゆよ
らりるれろ
わをん
がぎぐげご
ざじずぜぞ
だぢづでど
ばびぶべぼ
ぱぴぷぺぽ
ぁぃぅぇぉ
ゃゅょ
]]):gsub("%s",""))
local chars_t = {}
for c in string.gmatch(chars, utf8.charpattern) do
table.insert(chars_t, c)
end
local wanted_len = arg[1] and tonumber(arg[1]) or 100
local n = 0
repeat
local p = io.popen("openssl rand "..wanted_len)
local input = p:read("*a")
p:close()
for i = 1, #input do
local x = string.byte(input, i)
if x < #chars_t then
io.write(chars_t[x+1])
n = n + 1
if n >= wanted_len then
break
end
end
end
until n >= wanted_len
io.write("\n")
io.stderr:write(string.format("%d characters long.\n", n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment