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.

4
  • 1
    Possible duplicate of How to generate a random string in Ruby Commented Aug 22, 2019 at 8:17
  • You can get it without SecureRandom so: (0...8).map { (65 + rand(26)).chr }.join Commented Aug 22, 2019 at 8: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. Commented Aug 22, 2019 at 8: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? Commented Aug 22, 2019 at 14:53

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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 }

2 Comments

why are you doing +55
You’re right, should be +58 (I think; on train now, and braindead). Adding 58 brings hyphen to g, the first unused letter.
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.

3 Comments

"if you are generating an unlimited number of strings" – the OP isn't asking for an unlimited number of strings.
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.
I understand this as "every time the method is called". Maybe you're right though.
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)

1 Comment

Thanks majoj but i want in build method
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.