Skip to content

Instantly share code, notes, and snippets.

@surgicalcoder
Created August 10, 2020 12:30
Show Gist options
  • Select an option

  • Save surgicalcoder/215198b67c866852a7dfee3eb8e97b47 to your computer and use it in GitHub Desktop.

Select an option

Save surgicalcoder/215198b67c866852a7dfee3eb8e97b47 to your computer and use it in GitHub Desktop.
Deterministic "random" number
void Main()
{
// 1000 = Minimum number
// 65536 = maximum number
(1000 + GetInt64HashCode("John Reginald III esq.") % 65536).Dump();
}
static Int64 GetInt64HashCode(string strText)
{
Int64 hashCode = 0;
if (!string.IsNullOrEmpty(strText))
{
byte[] byteContents = Encoding.Unicode.GetBytes(strText);
var hash = new System.Security.Cryptography.SHA256CryptoServiceProvider();
byte[] hashText = hash.ComputeHash(byteContents);
Int64 hashCodeStart = BitConverter.ToInt64(hashText, 0);
Int64 hashCodeMedium = BitConverter.ToInt64(hashText, 8);
Int64 hashCodeEnd = BitConverter.ToInt64(hashText, 24);
hashCode = hashCodeStart ^ hashCodeMedium ^ hashCodeEnd;
}
if (hashCode < 0)
{
hashCode = hashCode * -1;
}
return (hashCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment