diff bin/randstr @ 586:ecff9d2c0500

Simple random string generator
author nanaya <me@myconan.net>
date Mon, 16 May 2016 18:21:24 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/randstr	Mon May 16 18:21:24 2016 +0900
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+
+require "securerandom"
+
+def random_base62(length)
+  group = ["A".."Z", "a".."z", "0".."9"].map(&:to_a).flatten
+
+  length.times.map do |x|
+    group[SecureRandom.random_number(group.length)]
+  end.join
+end
+
+length = ARGV[0].to_i
+length = 12 if length <= 0
+
+puts random_base62(length)