0

Is there any in built method in ruby which will generate unique alphabetic string every time(it should not have numbers only alphabets)?

i have tried SecureRandom but it doesn't provide method which will return string containing only alphabets.

Sanjay Salunkhe
  • 2,665
  • 5
  • 28
  • 52
  • 1
    Possible duplicate of [How to generate a random string in Ruby](https://stackoverflow.com/questions/88311/how-to-generate-a-random-string-in-ruby) – mrzasa Aug 22 '19 at 08:17
  • You can get it without SecureRandom so: (0...8).map { (65 + rand(26)).chr }.join – Sandra Aug 22 '19 at 08:19
  • You likely get much more sensible results if, whenever you have an unusual restriction, you explained why you have that restriction (i.e. what is your use case). It is even possible you have an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Amadan Aug 22 '19 at 08:23
  • 1
    Your question is not clear. Do you wish to generate a sequence of strings that are unique and in some sense random? Though you have referred to `SecureRandom` you have not stated that the strings are to be drawn randomly. To say that strings are drawn randomly you need to define the sample space. For example, are all strings to be the same specified length, or no longer than a specified length? Is the test of uniqueness case-indifferent? – Cary Swoveland Aug 22 '19 at 14:53

6 Answers6

4

SecureRandom has a method choose which:

[...] generates a string that randomly draws from a source array of characters.

Unfortunately it's private, but you can call it via send:

SecureRandom.send(:choose, [*'a'..'z'], 8)
#=> "nupvjhjw"

You could also monkey-patch Random::Formatter:

module Random::Formatter
  def alphabetic(n = 16)
    choose([*'a'..'z'], n)
  end
end

SecureRandom.alphabetic
#=> "qfwkgsnzfmyogyya"

Note that the result is totally random and therefore not necessarily unique.

Stefan
  • 109,145
  • 14
  • 143
  • 218
1

UUID are designed to have extremely low chance of collision. Since UUID only uses 17 characters, it's easy to change the non-alphabetic characters into unused alphabetic slots.

SecureRandom.uuid.gsub(/[\d-]/) { |x| (x.ord + 58).chr }
Amadan
  • 191,408
  • 23
  • 240
  • 301
1

Is there any in built method in ruby which will generate unique alphabetic string every time(it should not have numbers only alphabets)?

This is not possible. The only way that a string can be unique if you are generating an unlimited number of strings is if the string is infinitely long.

So, it is impossible to generate a string that will be unique every time.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • _"if you are generating an unlimited number of strings"_ – the OP isn't asking for an unlimited number of strings. – Stefan Aug 22 '19 at 11:30
  • The *precise* specification the OP has given is "generate unique alphabetic string **every time**" (**bold** emphasis mine). If you cannot guarantee an unlimited number of unique strings, then you cannot generate a unique string "every time", only sometimes, which does not meet the OP's specification. – Jörg W Mittag Aug 22 '19 at 12:03
  • I understand this as _"every time the method is called"_. Maybe you're right though. – Stefan Aug 22 '19 at 12:57
0
def get_random_string(length=2)
  source=("a".."z").to_a + ("A".."Z").to_a
  key=""
  length.times{ key += source[rand(source.size)].to_s }
  key
end

How about something like this if you like some monkey-patching, i have set length 2 here , please feel free to change it as per your needs

get_random_string(7)
Manoj Menon
  • 1,028
  • 8
  • 18
0

I used Time in miliseconds, than converted it into base 36 which gives me unique aplhanumeric value and since it depends on time so, it will be very unique.

Example: Time.now.to_f.to_s.gsub('.', '').ljust(17, '0').to_i.to_s(36) # => "4j26m4zm2ss"

Take a look at this for full answer: https://stackoverflow.com/a/72738840/7365329

Touqeer
  • 583
  • 1
  • 4
  • 19
-1

Try this one

length = 50
Array.new(length) { [*"A".."Z", *"a".."z"].sample }.join
# => bDKvNSySuKomcaDiAlTeOzwLyqagvtjeUkDBKPnUpYEpZUnMGF
dmrlps
  • 72
  • 1
  • 9