Skip to content

Instantly share code, notes, and snippets.

@yorzi
Created October 12, 2011 09:17
Show Gist options
  • Save yorzi/1280727 to your computer and use it in GitHub Desktop.
Save yorzi/1280727 to your computer and use it in GitHub Desktop.
random_token.rb
def random_token(number=10)
Digest::SHA1.hexdigest(Time.now.to_i.to_s)[1..number]
end
@namelessjon
Copy link

If you're on 1.9, why not

require 'securerandom'
SecureRandom.hex(10)

Much reduced chance of collisions should you get two requests in one second and much less possible to predict if you know the approximate time.

@yorzi
Copy link
Author

yorzi commented Oct 12, 2011

@namelessjon, you right, thanks! Your way is much better I think. My way relies on Time.now, but it should work correctly in my case :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment