{"id":16344477,"url":"https://github.com/siddhant-k-code/math.random-internals","last_synced_at":"2025-06-11T00:06:59.400Z","repository":{"id":230537602,"uuid":"779559234","full_name":"Siddhant-K-code/Math.random-internals","owner":"Siddhant-K-code","description":"Internals of Math.random()","archived":false,"fork":false,"pushed_at":"2024-03-30T07:14:23.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-18T08:38:08.732Z","etag":null,"topics":["internals","math","random","research"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Siddhant-K-code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-30T06:26:22.000Z","updated_at":"2024-03-30T07:36:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"30cf0dc9-9ca1-4e64-ab4c-307f09c5a8c7","html_url":"https://github.com/Siddhant-K-code/Math.random-internals","commit_stats":null,"previous_names":["siddhant-k-code/math.random-internals"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Siddhant-K-code%2FMath.random-internals","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Siddhant-K-code%2FMath.random-internals/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Siddhant-K-code%2FMath.random-internals/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Siddhant-K-code%2FMath.random-internals/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Siddhant-K-code","download_url":"https://codeload.github.com/Siddhant-K-code/Math.random-internals/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Siddhant-K-code%2FMath.random-internals/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259172972,"owners_count":22816560,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["internals","math","random","research"],"created_at":"2024-10-11T00:28:26.952Z","updated_at":"2025-06-11T00:06:59.379Z","avatar_url":"https://github.com/Siddhant-K-code.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `Math.random()` Internals\n\nWhen your mind wanders to the realms of generating random numbers, you might first land on the mystical `Math.random()` found in the world of JavaScript. Driven by curiosity, I embarked on an adventure to unravel the secrets behind this wizardry.\n\n## Crafting Magic with `Math.random()`\n\nAt the core of `Math.random()` lies an ancient spell known as Xorshift. This magical formula is both swift and light, consuming little of our mystical memory resources, though it must be noted, it conjures numbers that are almost, but not entirely, unpredictable.\n\n### From Ancient Scripts to Modern Spells: The Transformation to TypeScript\n\nMy quest led me to discover the original incantations of Xorshift in the ancient C scrolls. With a wave of my wand and a flick of my keyboard, I transformed these spells into the modern tongue of TypeScript, as shown below:\n\n```typescript\nclass Xorshift64State {\n  constructor(public a: number) {}\n}\n\nfunction xorshift64(state: Xorshift64State): number {\n  let x = state.a;\n  // The magical XOR dance begins here\n  x ^= x \u003c\u003c 7;\n  x ^= x \u003e\u003e\u003e 9;\n  state.a = x;\n  return x;\n}\n\nconst init = new Xorshift64State(1);\nconsole.log(xorshift64(init)); // The spell reveals the number 129\nconsole.log(xorshift64(init)); // Followed by the number 16417\n\nconst init2 = new Xorshift64State(2);\nconsole.log(xorshift64(init2)); // And for a different spell, 258\n```\n\nThese numbers are not mere figures; they are the offspring of our initial seeds. The magic lies in how these seeds transform, creating an illusion of randomness with each invocation.\n\n### Deciphering the Enchantment: The XOR Mystique\n\nIn our magical toolkit, XOR stands as the \"exclusive OR\" marked by the symbol `^=`. It's a spell that only reveals truth when exactly one of two bits is true. For example, in the realm of JavaScript:\n\n```javascript\nlet x = 10;\nx ^= 6; // The XOR spell is cast\nconsole.log(x); // Behold, the transformation to 12!\n```\n\nThis arcane knowledge extends to the shifting of bits, a maneuver that moves them left (`\u003c\u003c`) or right (`\u003e\u003e\u003e`), a simple yet profound manipulation of the very essence of numbers.\n\n### Following the Magical Thread\n\nLet's dive deeper, tracing the path of a number as it undergoes transformation through our XOR spells:\n\n```javascript\nlet x = 8; // The chosen one\nx ^= x \u003c\u003c 7; // Shifted left and XOR'd\nx ^= x \u003e\u003e\u003e 9; // Then shifted right, unsigned, and XOR'd again\nconsole.log(x); // The magic concludes, revealing 1034\n```\n\nEach step is a dance, shifting, XOR'ing, and transforming, leading to results that dazzle and surprise.\n\n## Reimagining Math.random(): A Wizard's Experiment\n\nInspired, I ventured to reimagine `Math.random()` itself, employing the current time as our seed in a concoction of shifts and XORs:\n\n```javascript\nlet seed = Date.now(); // The seed from the sands of time\n\nfunction Random() {\n  seed ^= seed \u003c\u003c 7; // Our familiar XOR and shift spells\n  seed ^= seed \u003e\u003e\u003e 9;\n  return Math.min(1, Math.abs(seed) / 2000000000); // Ensuring our result stays within bounds\n}\n\nconsole.log(Random());\nconsole.log(Random());\nconsole.log(Random());\n```\n\nThough enchanting, consider this more a wizard's playful experiment than a spell for daily use.\n\n## In Conclusion: The Quest Continues\n\nBeyond Xorshift, myriad other spells of pseudo-random generation beckon, from the linear congruences to the arcane arts of subtractive generation. For those in search of the most impenetrable randomness, the cryptographically secure `crypto.getRandomValues()` offers a fortress of unpredictability.\n\nThus, we see that behind each seemingly simple `Math.random()`, there lies a world of algorithms, each with its own lore, waiting for the curious and the bold.\n\n## Recommended Readings\n\n- [Crypto: `getRandomValues()` method](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiddhant-k-code%2Fmath.random-internals","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiddhant-k-code%2Fmath.random-internals","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiddhant-k-code%2Fmath.random-internals/lists"}