Epus
Epus

Reputation: 49

Random string generation with same pattern

I want to generate a random string with the pattern:

number-number-letter-SPACE-letter-number-number

for example "81b t15", "12a x13". How can I generate something like this? I tried generating each char and joining them into one string, but it does not look efficient.

Upvotes: 1

Views: 86

Answers (4)

Kimmo Lehto
Kimmo Lehto

Reputation: 6041

Ok here's another entry for the competition :D

module RandomString
  LETTERS      = (("A".."Z").to_a + ("a".."z").to_a)
  LETTERS_SIZE = LETTERS.size
  SPACE        = " "
  FORMAT       = [:number, :letter, :number, :space, :letter, :number, :number]

  class << self
    def generate
      chars.join
    end

    def generate2
      "#{number}#{letter}#{number} #{letter}#{number}#{number}"
    end

    private

    def chars
      FORMAT.collect{|char_class| send char_class}
    end

    def letter
      LETTERS[rand(LETTERS_SIZE)]
    end

    def number
      rand 10
    end

    def space
      SPACE
    end
  end
end

And you use it like:

50.times { puts RandomString.generate }

Out of curiosity, I made a benchmark of all the solutions presented here. Here are the results:

JRuby:

       user     system      total        real
kimmmo   1.490000   0.000000   1.490000 (  0.990000)
kimmmo2  0.600000   0.010000   0.610000 (  0.479000)
sawa     0.960000   0.040000   1.000000 (  0.533000)
hp4k     2.050000   0.230000   2.280000 (  1.234000)
brian   17.700000   0.170000  17.870000 ( 14.867000)

MRI 2.0

       user     system      total        real
kimmmo   0.900000   0.000000   0.900000 (  0.908601)
kimmmo2  0.410000   0.000000   0.410000 (  0.406443)
sawa     0.570000   0.000000   0.570000 (  0.568935)
hp4k     4.940000   0.000000   4.940000 (  4.945404)
brian   25.860000   0.010000  25.870000 ( 25.870011)

Upvotes: 1

hp4k
hp4k

Reputation: 356

You can do it this way

(0..9).to_a.sample(2).join + ('a'..'z').to_a.sample + " " + ('a'..'z').to_a.sample + (0..9).to_a.sample(2).join

Upvotes: 0

Brian
Brian

Reputation: 4930

Have you looked at randexp gem

It works like this:

> /\d\d\w \w\d\d/.gen
=> "64M c82"

Upvotes: 3

sawa
sawa

Reputation: 168101

Nums = (0..9).to_a
Ltrs = ("A".."Z").to_a + ("a".."z").to_a
def rand_num; Nums.sample end
def rand_ltr; Ltrs.sample end

"#{rand_num}#{rand_num}#{rand_ltr} #{rand_ltr}#{rand_num}#{rand_num}"
# => "71P v33"

Upvotes: 4

Related Questions