Last active
February 26, 2025 16:59
-
-
Save avilde/3736a903560b35fd587d213a3f79fad7 to your computer and use it in GitHub Desktop.
Mulberry 32-bit random seed generator - Typescript
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
/* eslint-disable no-bitwise, no-param-reassign, operator-assignment */ | |
// Mulberry32 - 32-bit random seed generator | |
// Source: https://github.com/bryc/code/blob/master/jshash/PRNGs.md#mulberry32 | |
/** | |
* Function is used to get the same random value every time to ensure | |
* data is the same in unit tests and screenshot tests for storybook | |
* @param seed | |
* @returns random number based on input seed | |
*/ | |
export function createRandomWithSeed(seed: number): () => number { | |
return () => { | |
seed |= 0; | |
seed = seed + 0x6D2B79F5 | 0; | |
let imul = Math.imul(seed ^ seed >>> 15, 1 | seed); | |
imul = imul + Math.imul(imul ^ imul >>> 7, 61 | imul) ^ imul; | |
return ((imul ^ imul >>> 14) >>> 0) / 4294967296; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment