How random is random, Ruby on Rails UUID's 2

Posted by Joel Jensen Mon, 24 Dec 2007 20:08:21 GMT

I needed a good random number. Some opensource login generator I used had

They made a random number like this.
Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )

I don't know if thats really random. I think it is.

UUID's are unique by definition. I have a ruby uuid library. 

Ruby is slow for UUID's
Mysql is fast.

# a function that uses mysql > 4.1 to generate uuid's. Just stick it in your model.
def uuid()
   sql = "SELECT UUID()"   
   record = connection.select_one(sql)
   return record['UUID()']
end
Trackbacks

Use the following link to trackback from your own site:
http://www.nervetree.com/trackbacks?article_id=how-random-is-random-ruby-on-rails-uuids&day=24&month=12&year=2007

Comments

Leave a comment

  1. shawn 7 months later:

    why do you need a uuid for every login? isnt a simple counter of just their username.nick good enough???

  2. irv 9 months later:

    Hey! Thanks for this. It’s simple and fast and gave me just what I wanted without any fuss. I’m importing items that may or may not have a uuid. I put this (slightly modified) in as a before_save to check and add a uuid if there isn’t one already. Works like a charm.

Comments