Skip to content

Instantly share code, notes, and snippets.

@tyuki39
Created March 26, 2011 14:38
Show Gist options
  • Save tyuki39/888331 to your computer and use it in GitHub Desktop.
Save tyuki39/888331 to your computer and use it in GitHub Desktop.
[0-9a-zA-Z_]からなる16文字のランダム文字列
// new Random と nextIntを使用
[*0..9,*'a'..'z',*'A'..'Z','_'].with{a->println new Random().with{(1..16).collect{a[nextInt(a.size())]}.join()}}
// Math.randomを使用
[*0..9,*'a'..'z',*'A'..'Z','_'].with{a->println((1..16).collect{a[(int)Math.random()*a.size()]}.join())}
// def と println版
def a=[*0..9,*'a'..'z',*'A'..'Z','_'];println((1..16).collect{a[(int)Math.random()*a.size()]}.join())
@fumokmm
Copy link

fumokmm commented Mar 26, 2011

ちなみにjoinはsumにすると1バイトお得です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment