Created
March 2, 2012 20:21
-
-
Save edwardloveall/1961021 to your computer and use it in GitHub Desktop.
Create a unique "string hash" based off of a seed string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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