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.

Creating randomness Without Math.random

78 pointsby healeycodesalmost 5 years ago

9 comments

kqralmost 5 years ago
There are a few algorithms I think are simple enough to memorise yet seem universally useful enough to be bound to be good to know by heart at some point or other. LCG is one of them!<p>Something else that might be useful to know are methods to transform the uniform output of LCG into various other types of distribution shapes:<p>When U is uniform between 0 and 1 exclusive,<p>One can get an Exp(lambda) distributed variable with -log(U)&#x2F;lambda<p>One can get a heavy-tailed Pareto(m,a) distributed variable with m&#x2F;(1-u)^(1&#x2F;a)<p>I&#x27;m still missing something easily memorable for bell-shaped distributions. The normal distribution would be nice because it&#x27;s so easy to transform N(0,1) into a distribution with arbitrary parameters, but there&#x27;s no simple way to compute the standard normal.
评论 #23818563 未加载
评论 #23823960 未加载
评论 #23819219 未加载
syspecalmost 5 years ago
Great article, I love &quot;because why not articles&quot;.<p>Checkout Perlin Noise, for when you need &quot;smooth&quot; and &quot;natural&quot; looking random numbers (making terrain, skies, waves, etc)<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=8ZEMLCnn8v0" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=8ZEMLCnn8v0</a>
chrismorganalmost 5 years ago
Deterministic pseudorandomness is really useful for seeding dummy data, data that you’d like to be fairly random (though it doesn’t need to be high-quality randomness, so LCG is fine) but consistent across runs&#x2F;page loads.
aloisdgalmost 5 years ago
Good stack overflow answer about js implementation of pseudorandom number generator: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;47593316&#x2F;1248177" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;47593316&#x2F;1248177</a>
trevoristallalmost 5 years ago
I just watched a really good video explaination of this the other day: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4sYawx70iP4" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4sYawx70iP4</a>
ggmalmost 5 years ago
The real value is the reference to the API calls which inherently represent less predictable seed state. If you can&#x27;t call out, then these are acheing to be used alongside some kind of introspection into the runtime.
Someonealmost 5 years ago
<i>“Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls.”</i><p>Nitpick: it would surprise me if any implementation implemented this requirement to the letter.<p>I think neither of the claims (FTA) “PRNGs are deterministic” and “A PRNG eventually repeats its sequence” are strictly required (for the first you can’t seed Javascript’s Math.Random, so there is no need to be able to regenerate a sequence; for the second, one could generate good pseudo random sequences that never repeat), but the implementations I’m aware of all have those properties, and use fixed-size state.<p>Given that, the number of PRNGs whose period is at most P is finite. So, one could exhaust that set by creating realms and random number sequences in each of them in a loop (a looooong loop)<p>The only ways to avoid generating a copy would be to gradually move to generators with more state or to crash.
评论 #23815915 未加载
评论 #23825385 未加载
评论 #23818174 未加载
评论 #23818053 未加载
评论 #23815697 未加载
michael-axalmost 5 years ago
<a href="https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2017&#x2F;09&#x2F;21&#x2F;" rel="nofollow">https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2017&#x2F;09&#x2F;21&#x2F;</a>
galaxyLogicalmost 5 years ago
It would be great if JavaScript had also a built-in seedable random number generator.
评论 #23817813 未加载