Long Hoang

People often call me Tobi, but you can call me tonight!

Navigation
 » Home
 » About Me
 » Github
 » XML Feed

Generate n unique characters

11 Jun 2019 » ruby
# generator.rb

ASCII = ('!'..'~')
ALPHA = ASCII.grep(/[[:alpha:]]/)

class Generator
  def self.generate(length: 10)
    alpha = ALPHA.dup
    (1..length).map do
      alpha.delete_at(rand(alpha.size))
    end.join
  end
end


# generator_test.rb

require "minitest/autorun"
require "./generator"

class GeneratorTest < Minitest::Test
  def test_unique
    50.times do
      uniq_string = Generator.generate(length: 20)
      assert_match /^[a-zA-Z]+$/, uniq_string
      assert_equal uniq_string.size, uniq_string.chars.uniq.size
    end
  end
end