TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Exploring Randomness in JavaScript

26 pointsby kiyanwang11 months ago

5 comments

jsheard11 months ago
The behaviour of Math.random() is technically browser specific since the algorithm isn&#x27;t specified, but I think all the big engines have settled on xorshift128+. That&#x27;s probably good enough for most non-crypto things.<p>Still, if you want consistent quality and speed across all browsers current and future, you&#x27;re better off bundling your own PRNG code. That&#x27;s also necessary if you want a repeatable sequence from a specific seed value, neither of the browser built-in methods support that.
评论 #40840769 未加载
blixt11 months ago
I was also exploring randomness in JS at some point and found lots of interesting things! One was that the Alea PRNG algorithm[1] by Johannes Baagøe performed faster on JavaScript&#x27;s floating point numbers. Another was that Dieharder[2] is a really fun tool to test PRNGs. I also made an attempt at consolidating other PRNG methods which were not great[3] and that led me to other people who had done the same[4].<p>And finally I tried to make a nicer Dieharder wrapper and a simple PRNG library, but lord knows how relevant it is anymore: <a href="https:&#x2F;&#x2F;github.com&#x2F;blixt&#x2F;js-arbit">https:&#x2F;&#x2F;github.com&#x2F;blixt&#x2F;js-arbit</a><p>I guess in this archaeological dig I also found how many useful resources on the internet disappear in less than a decade.<p>[1]: <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20120502223108&#x2F;http:&#x2F;&#x2F;baagoe.com&#x2F;en&#x2F;RandomMusings&#x2F;javascript&#x2F;" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20120502223108&#x2F;http:&#x2F;&#x2F;baagoe.com...</a> (Baagøe&#x27;s original site is down)<p>[2]: <a href="https:&#x2F;&#x2F;rurban.github.io&#x2F;dieharder&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rurban.github.io&#x2F;dieharder&#x2F;</a> (old site is dead, though here&#x27;s a web archive link: <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20170609075452&#x2F;http:&#x2F;&#x2F;www.phy.duke.edu&#x2F;~rgb&#x2F;General&#x2F;dieharder.php" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20170609075452&#x2F;http:&#x2F;&#x2F;www.phy.du...</a>)<p>[3]: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;blixt&#x2F;f17b47c62508be59987b" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;blixt&#x2F;f17b47c62508be59987b</a> (Don&#x27;t use this)<p>[4]: <a href="https:&#x2F;&#x2F;github.com&#x2F;nquinlan&#x2F;better-random-numbers-for-javascript-mirror">https:&#x2F;&#x2F;github.com&#x2F;nquinlan&#x2F;better-random-numbers-for-javasc...</a> (this is mainly a mirror of Baagøe&#x27;s wiki)
montroser11 months ago
Here&#x27;s my favorite deterministic PRNG optimizing for implementation simplicity:<p><pre><code> function random() { random._s = random._s || 11224; &#x2F;&#x2F; seed return (random._s = random._s * 16807 % 2147483647) &#x2F; 2147483646; } </code></pre> Wikipedia says this approach has been around at least since the 1950s.<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Linear_congruential_generator" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Linear_congruential_generator</a>
iamsanteri11 months ago
What would be the best way for me to create a truly random approach to sampling from a specified distribution in my Monte Carlo simulator app? In my current MVP Beta app I’m just using Math.random() as I cannot access or install any external libraries. How would you go about implementing something more robust in something like, say, Apps Script (a Google proprietary derivative of JavaScript for cloud apps)?
评论 #40841486 未加载
wildrhythms11 months ago
Nice article, and reminds me of my personally most used method: window.crypto.randomUUID()