Sam
Sam

Reputation: 5250

How to generate some random addresses in ruby?

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

DEUbQpgKyGDAjNqRXcpMYVD3HunVjCrH1G

DHcjoCTL2rHnAuKmWam64QfZv6H3DG3S6f

DSM1eJ6yXiaATHGXVmmznF1wuEuEzYvGHz

Upvotes: 0

Views: 235

Answers (3)

secador de pelo
secador de pelo

Reputation: 699

For random folder names i do

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

Upvotes: 0

user810992
user810992

Reputation: 54

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"

Upvotes: 1

Florian Widtmann
Florian Widtmann

Reputation: 514

such a thing?

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

or How to generate a random string in Ruby

Upvotes: 0

Related Questions