0

How to generate some random addresses(string) in ruby?

DEUbQpgKyGDAjNqRXcpMYVD3HunVjCrH1G

DHcjoCTL2rHnAuKmWam64QfZv6H3DG3S6f

DSM1eJ6yXiaATHGXVmmznF1wuEuEzYvGHz

Sam
  • 5,040
  • 12
  • 43
  • 95
  • I would just use SecureRandom. See this post: http://stackoverflow.com/a/1619602/812503 – Corey Mar 14 '14 at 13:51

3 Answers3

1

If you want it to be unique, you can use SecureRandom. The simplest way would be using

SecureRandom.uuid

which generates ID like

irb(main):001:0> SecureRandom.uuid 
=> "0d218853-5ef8-46df-85b1-e7e7af18c0ba"

If you want it guaranteed to be unique, you may want to add timestamp:

Digest::MD5.hexdigest "#{SecureRandom.hex(10)}-#{DateTime.now.to_s}"

which generates

irb(main):002:0> Digest::MD5.hexdigest "#{SecureRandom.hex(10)}-#{DateTime.now.to_s}"
=> "15aaf4a73969c67afccdfdaf629a310e"
user810992
  • 54
  • 2
0

such a thing?

Digest::MD5.hexdigest('foo') # => "acbd18db4cc2f85cedef654fccc4a4d8" 

or How to generate a random string in Ruby

Community
  • 1
  • 1
Florian Widtmann
  • 524
  • 4
  • 15
0

For random folder names i do

timestamp = Time.new.strftime("%Y-%m-%dT%H-%M-%S%z")
"#{timestamp}_#{("%04x" % rand(16**4))}"
secador de pelo
  • 687
  • 5
  • 26