Skip to content

Instantly share code, notes, and snippets.

@edwardloveall
Created March 2, 2012 20:21
Show Gist options
  • Save edwardloveall/1961021 to your computer and use it in GitHub Desktop.
Save edwardloveall/1961021 to your computer and use it in GitHub Desktop.
Create a unique "string hash" based off of a seed string
def random_str(length, options = {})
srand(options[:seed].int_hash)
(0...length).map{97.+(rand options[:range]).chr}.join
end
# random_str(5, {seed: "edward", range: 5})
# => "adbae"
# random_str(10, {seed: "edward", range: 26})
# => "yhwtfxpjqe"
require 'digest/md5'
class String
def int_hash
Digest::MD5.hexdigest(self).to_i(16)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment