Created
January 30, 2026 12:12
-
-
Save samermurad/a099a67b2f6e4527a8fcc5c997a4275f to your computer and use it in GitHub Desktop.
Randomize a string chars using the mulberry32 deterministic randomness in JavaScript
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
| function mulberry32(a) { | |
| return function () { | |
| let t = a += 0x6D2B79F5; | |
| t = Math.imul(t ^ (t >>> 15), t | 1); | |
| t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | |
| return ((t ^ (t >>> 14)) >>> 0) / 4294967296; | |
| }; | |
| } | |
| const main = (str) => { | |
| const rng = mulberry32(Math.random() * str.length + 1); | |
| let numSet = new Set() | |
| while (numSet.size < str.length) numSet.add(Math.round(rng() * (str.length - 1))); | |
| let newStr = new Array(...numSet).map(n => str[n]).join('') | |
| console.log(newStr) | |
| return newStr; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment