TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Predictably Random

24 点作者 Fudgel将近 6 年前

9 条评论

OskarS将近 6 年前
Hubba bubba. This is maybe not the article to read if you want to learn how to implement randomness correctly. There&#x27;s... many things wrong with it.<p>Of course randomA is terrible, you&#x27;re modding it with 16! It&#x27;s never going to have a period longer than 16 like that! Same with randomB, just with a slightly larger number. Both of these are just bog-standard linear congruential generators. LCGs are pretty bad for anything &quot;serious&quot;, but for picking Tetris pieces they&#x27;re fine, and they&#x27;re easy to implent (or at least I thought they were, but then you see an article like this where one of them has period 16...). I don&#x27;t immediately recognie randomC(), but it also looks terrible. Just shifting some bits around and adding a counter. At least it has some internal state so it it&#x27;s not stupidly periodic.<p>I haven&#x27;t read up on what&#x27;s the issue with V8&#x27;s Math.random(), but I would have to imagine it&#x27;s superior to all these three. I&#x27;m also sure there&#x27;s plenty of excellent randomness libraries (and plenty of terrible ones) on NPM.<p>Also, this kind of visual inspection will weed out truly garbage PRNGs, but it&#x27;s not a good test in general. Testing for pseudo-randomness is hard, and best left to people who know what they are doing.<p>Also also: for Tetris, you shouldn&#x27;t just pick pieces at random, that&#x27;s not how Tetris works nowadays. Tetris works by putting all 7 pieces in a bag, and then pulling them out at random. When the bag is empty, you fill it again with the seven pieces and start over. More info here: [0]. If you&#x27;re new to programming, implementing the 7-bag system properly is a good little challenge, you&#x27;ll get to learn all about the Fisher-Yates shuffle [1].<p>[0]: <a href="https:&#x2F;&#x2F;tetris.fandom.com&#x2F;wiki&#x2F;Random_Generator" rel="nofollow">https:&#x2F;&#x2F;tetris.fandom.com&#x2F;wiki&#x2F;Random_Generator</a><p>[1]: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fisher%E2%80%93Yates_shuffle" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fisher%E2%80%93Yates_shuffle</a>
评论 #20685940 未加载
评论 #20685730 未加载
评论 #20689829 未加载
评论 #20685679 未加载
soVeryTired将近 6 年前
RNGs are like crypto: don&#x27;t roll your own! There&#x27;s some really deep number theory behind many of them. Every RNG I&#x27;ve seen lets you control the seed, so I&#x27;m not sure what he gains by writing his own.
评论 #20685752 未加载
FabHK将近 6 年前
The article is amateurish, and it neither reflects the range and complexity of issues surrounding PRNGs, nor does it offer any good advice (except maybe &quot;don&#x27;t just use the first PRNG you come across&quot;).<p>A better introduction is the PCG website (it&#x27;s biased towards PCG, but a) that&#x27;s not a bad choice for many use cases, and b) it raises and discusses many issues regarding PRNG choice).<p><a href="http:&#x2F;&#x2F;www.pcg-random.org" rel="nofollow">http:&#x2F;&#x2F;www.pcg-random.org</a><p>EDIT to add: and FWIW, any serious PRNG of course allows seeding, and then the issue boils down to choosing a shared seed.
strenholme将近 6 年前
Which random number generator to use can be a heated discussion, with strong opinions. math.random() and rand() can have issues using poor random number generators like LCGs instead of good random number generators.<p>The default “good” random number generator is the “Mersenne Twister” (most of the time, the 32-bit MT19937 version). It generates numbers which look quite random while not being cryptographically strong (the problem with crypto-strong generators is that they tend to be slower, and can still cause legal issues in some jurisdictions). A lot of languages use this for the default random number generator.<p>Some people really like using xorshift generators; with the right parameters, xorshift can generate high quality numbers while being much simpler than MT19937. One version of this which some use is JKISS32; it’s small and makes good numbers.<p>Random number generators can be tested using a series of tests called “dieharder”; another test for RNGs is “bigcrush” which takes hours, sometimes days, to fully run. These tests make sure the random number generators are statistically random, using a large number of tests of the generator.<p>My favorite pseudo random number generator is a cryptographically strong one: RadioGatún[32]. It’s simple, fast, doesn’t require special seeding of its state (unlike MT19937), and allows the seed to be an arbitrary string instead of just being a number. It passes all dieharder tests, but I haven’t had a chance to test it with bigcrush yet. I have a GitHub repo with implementations of it I have done in various languages (C, Python2, Python3, Javascript, C++, etc.)
评论 #20685844 未加载
Pinbenterjamin将近 6 年前
Kind of unrelated, but I recently tested out a scenario for the Dotnet Environment that worked really well;<p>I created a &#x27;Random&#x27; service that lives for the length of the execution of the application.<p>This service has an instance of Random that persists with the object, and exposes simple methods with min&#x2F;max parameters.<p>I register the service in a unity container, and then immediately resolve it, causing the Random type inside of the random service to instantiate.<p>Then anywhere I want to generate a random number, I inject that service.<p>This works because, as long as you persist a single instance of &#x27;Random&#x27;, two calls to &#x27;Next&#x27; or &#x27;NextDouble&#x27; won&#x27;t result in the same number.
perspective1将近 6 年前
I&#x27;m not in the cryptography or random space beyond using libraries, but these visualizations are very clever. It&#x27;s easy to see major problems.<p>edit: It shows small-periods but otherwise it&#x27;s not all that useful (see below).
评论 #20685463 未加载
willis936将近 6 年前
Why not use RDRAND?<p>Also you can always choose well-established, arbitrarily complex psuedo random algorithms. If you want to guarantee low correlation you could use PRBS (whatever shift amount you need, 31 may be enough for most scenarios) with the same shift register values and seeds that are maximally equidistant.
评论 #20685763 未加载
grayed-down将近 6 年前
Test and debate all you want, but if your application needs random data and it has access or intermittent access to the internet, use the Cesium isotope 137 :)<p><a href="https:&#x2F;&#x2F;www.fourmilab.ch&#x2F;hotbits&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.fourmilab.ch&#x2F;hotbits&#x2F;</a>
woliveirajr将近 6 年前
TL;DR: random is hard because computers are deterministic; we use Pseudo-Random, and each function that generates pseudo-random must be tested to see how random the results are.