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.

The random number generator of DOOM

115 pointsby danshapiroalmost 10 years ago

16 comments

perlinalmost 10 years ago
In 1982 (several years before DOOM), Ken Perlin invented an algorithm meant to produce random numbers that more closely resembled a human&#x27;s interpretation of &quot;random&quot; numbers (versus those generated by computers). Humans, it seemed, tended to choose a lot of different numbers, whereas random numbers from a CPU actually produced lots of repeating sequences. His work in the field was vital to developing the first 3D shaded graphics in a Hollywood film (Tron), for which he won an Academy Award for Technical Achievement.<p>Perlin Noise is still used to this day for generating clouds, natural-looking terrains, and other textures that are pleasing to the human brain.<p>Python implementation: <a href="https:&#x2F;&#x2F;github.com&#x2F;caseman&#x2F;noise&#x2F;blob&#x2F;master&#x2F;perlin.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;caseman&#x2F;noise&#x2F;blob&#x2F;master&#x2F;perlin.py</a><p>Excellent talk by its creator: <a href="http:&#x2F;&#x2F;www.noisemachine.com&#x2F;talk1&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.noisemachine.com&#x2F;talk1&#x2F;</a>
评论 #9810302 未加载
评论 #9810180 未加载
评论 #9810265 未加载
评论 #9810417 未加载
pdwalmost 10 years ago
There&#x27;s enough old Softdisk&#x2F;id Software code released that you can trace the evolution of this RNG.<p>First, check out Catacomb (1989). I think this is the oldest of Carmack&#x27;s games for which source code has been released. This game uses a lagged fibonacci generator:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;FlatRockSoft&#x2F;Catacomb&#x2F;blob&#x2F;master&#x2F;CATASM.ASM#L397" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FlatRockSoft&#x2F;Catacomb&#x2F;blob&#x2F;master&#x2F;CATASM....</a><p>This makes sense, it&#x27;s a generator that requires little memory and no expensive operations such as multiplications. You&#x27;ll probably also find it in Softdisk&#x27;s Apple II releases.<p>Carmack&#x27;s first 3D game is Hovertank 3D, released in 1991. The LFG still exists, but now a &quot;table-based RND generator&quot; also appears. This seems to be the first appearance of the &quot;Doom RNG&quot;.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;FlatRockSoft&#x2F;Hovertank3D&#x2F;blob&#x2F;master&#x2F;IDASM.ASM#L698" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FlatRockSoft&#x2F;Hovertank3D&#x2F;blob&#x2F;master&#x2F;IDAS...</a><p>The LFG is used when setting up the map, while the table-based is used for enemy AI during gameplay. Obviously every cycle counts when trying to do 3D on a 286.<p>The same year also saw the release of Keen Dreams, in which only the table-based RNG survives:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;keendreams&#x2F;keen&#x2F;blob&#x2F;master&#x2F;id_us_a.asm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;keendreams&#x2F;keen&#x2F;blob&#x2F;master&#x2F;id_us_a.asm</a><p>(The state variables of the LFG are still defined, but the code is missing.)
评论 #9811443 未加载
paulannesleyalmost 10 years ago
Related: <a href="http:&#x2F;&#x2F;jmtd.net&#x2F;log&#x2F;deterministic_doom&#x2F;" rel="nofollow">http:&#x2F;&#x2F;jmtd.net&#x2F;log&#x2F;deterministic_doom&#x2F;</a><p>Via: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9429889" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9429889</a>
评论 #9810465 未加载
pgyalmost 10 years ago
There was a post about tampering with randomness in Doom some time ago with some interesting hn comments explaining the reason for this design: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9429889" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9429889</a>
GaiusCoffeealmost 10 years ago
I hope I don&#x27;t sound too dumb, but how does it work, exactly? It doesn&#x27;t look random to me, more like an unordered (but repeating) sequence..
评论 #9810073 未加载
评论 #9810095 未加载
评论 #9811167 未加载
评论 #9810286 未加载
评论 #9810573 未加载
sytelusalmost 10 years ago
This is great RNG for games although most computer scientists would strongly disagree. We focus so heavily on having huge cycle length for RNG that we forget that human memory is not all that great at remembering exact sequence of even just handful of random numbers, let alone 255 of them. So you don&#x27;t need RNGs with guarantees of huge cycles for gaming scenarios like adding error in to projectile&#x27;s path. The advantage of this RNG is that it&#x27;s blazingly fast (think all the cache hits!). Of course this would be useless for any real work on physics simulation or weather simulation etc.
tbrakealmost 10 years ago
Curious. Just eyeballing I see there&#x27;s no 1, 2 and 222 are repeated as well as 36 and 136. There are probably more.<p>If there&#x27;s a purpose behind the table being constructed with those omissions&#x2F;repetitions it&#x27;s lost on me.
评论 #9810098 未加载
评论 #9813317 未加载
评论 #9814081 未加载
megablastalmost 10 years ago
random_number = 4&#x2F;&#x2F; just tested it with a dice
评论 #9810510 未加载
frontforalmost 10 years ago
Interestingly, Half-Life (released in 1998) uses a slightly more complex table-based generator:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ValveSoftware&#x2F;halflife&#x2F;blob&#x2F;master&#x2F;dlls&#x2F;util.cpp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ValveSoftware&#x2F;halflife&#x2F;blob&#x2F;master&#x2F;dlls&#x2F;u...</a><p>Note that this is only used for certain parts of the game. For others it uses a closed source non-table based generator.
TomGullenalmost 10 years ago
Interested as to why this method was picked over say doing &quot;MSPlayed % 256&quot;
评论 #9810848 未加载
userbinatoralmost 10 years ago
That seems like a very unusual (and large) way to produce a pseudorandom sequence. Why not a linear congruential generator or LFSR? They have approximately the same state storage requirements, but much less static data.
评论 #9810323 未加载
评论 #9810321 未加载
kdrakonalmost 10 years ago
For a video game, I suppose it was random &#x27;enough&#x27;.
vorticoalmost 10 years ago
What does the P and M stand for in the function names?
评论 #9810458 未加载
i_have_to_speakalmost 10 years ago
Mandatory xkcd mention: <a href="https:&#x2F;&#x2F;xkcd.com&#x2F;221&#x2F;" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;221&#x2F;</a>
xiaqalmost 10 years ago
If you realize that any RNG on [0,256) always has a cycle of at most 256, this is actually pretty decent.
评论 #9810820 未加载
netheril96almost 10 years ago
Yet another random number generator that depends on global mutable state and therefore unsafe to be used in multithreaded context.
评论 #9810606 未加载