Skip to content

Instantly share code, notes, and snippets.

@MitsunChieh
Last active November 26, 2015 07:52
Show Gist options
  • Save MitsunChieh/1fd07b14fa1957f0bd52 to your computer and use it in GitHub Desktop.
Save MitsunChieh/1fd07b14fa1957f0bd52 to your computer and use it in GitHub Desktop.
產生隨機字串
# 09azAZ
# n = 字串的長度
range = [*0..9,*'a'..'z',*'A'..'Z'];Array.new(n){ range.sample }.join
# 09az
# 長度 128
rake secret
# 好玩的寫法
# 09az
# n = 字串的「最大長度」(有機會產生長度小於 n 的字串)
rand(36**n).to_s(36) # 把數字轉成 36 進位
rand(36**(n-1)..36**n).to_s(36) # 確保長度 = n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment