Created
August 10, 2020 12:30
-
-
Save surgicalcoder/215198b67c866852a7dfee3eb8e97b47 to your computer and use it in GitHub Desktop.
Deterministic "random" number
This file contains hidden or 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
| 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