Last active
November 26, 2015 07:52
-
-
Save MitsunChieh/1fd07b14fa1957f0bd52 to your computer and use it in GitHub Desktop.
產生隨機字串
This file contains hidden or 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
# 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