annotate bin/randstr @ 684:6010e722b5da

[irbrc] Disable syntax highlighting as it slows down things Reference: https://github.com/ruby/irb/issues/292
author nanaya <me@nanaya.pro>
date Wed, 14 Sep 2022 19:06:13 +0900
parents ecff9d2c0500
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
586
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
1 #!/usr/bin/env ruby
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
2
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
3 require "securerandom"
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
4
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
5 def random_base62(length)
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
6 group = ["A".."Z", "a".."z", "0".."9"].map(&:to_a).flatten
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
7
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
8 length.times.map do |x|
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
9 group[SecureRandom.random_number(group.length)]
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
10 end.join
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
11 end
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
12
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
13 length = ARGV[0].to_i
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
14 length = 12 if length <= 0
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
15
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
16 puts random_base62(length)