Created
August 1, 2022 21:34
-
-
Save jonathancore/65ef601aae25b5b12836b9b8a6cb8452 to your computer and use it in GitHub Desktop.
Hash string to 32bit integer. Good for seeding prngs while also working as a tolerable prng itself.
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
'not sure who original author was. possibly Baggoe | |
function Mash() { | |
const initialize = 0xefc8249d; | |
let first = true; | |
return (seed = ' ', reset = first) => { | |
if(reset) [n, first] = [initialize, false]; | |
seed = String(seed); | |
let i = seed.length; | |
while (i--) { | |
//Basically, getting a value from each characters charCode, multiplying by a magic number, | |
//then hacking off the integer value. Rinse and repeat. | |
let h = (n += seed.charCodeAt(i)) * 0.02519603282416938; | |
h *= (h -= (n = h >>> 0)); | |
n += (h -= (n = h >>> 0)) * 0x100000000; | |
} | |
// uncomment below to have decimal values returned between 0 - 1. | |
return (n >>> 0) /* * 2.3283064365386963e-10 */ ; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment