4 ms·
Hubba bubba. This is maybe not the article to read if you want to learn how to implement randomness correctly. There's... many things wrong with it. Of course
by OskarS 7y ago
Hubba bubba. This is maybe not the article to read if you want to learn how to implement randomness correctly. There's... many things wrong with it.
Of course randomA is terrible, you're modding it with 16! It'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 "serious", but for picking Tetris pieces they're fine, and they'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'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's not stupidly periodic.
I haven't read up on what's the issue with V8's Math.random(), but I would have to imagine it's superior to all these three. I'm also sure there's plenty of excellent randomness libraries (and plenty of terrible ones) on NPM.
Also, this kind of visual inspection will weed out truly garbage PRNGs, but it's not a good test in general. Testing for pseudo-randomness is hard, and best left to people who know what they are doing.
Also also: for Tetris, you shouldn't just pick pieces at random, that'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're new to programming, implementing the 7-bag system properly is a good little challenge, you'll get to learn all about the Fisher-Yates shuffle [1].
[0]: https://tetris.fandom.com/wiki/Random_Generator https://tetris.fandom.com/wiki/Random_Generator
[1]: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
- willis936 7y agoOr as they call it in the biz: sample without replacement.
- OskarS 7y agoEssentially, yes, but the idea here is to create an endless stream but with just 7 pieces, so you restart the sampling after every bag.
- defertoreptar 7y ago> you shouldn't just pick pieces at random, that's not how Tetris works nowadays. It's a purely abstract game with endless possibilities for how it can be made. That's what makes it timeless. There's no reason you have to use bag randomizer in your own personal projects. That is, unless you feel some strange need to check off boxes to comply with what the current IP holders happen to prescribe at a particular moment in time. In fact, there's an argument for using a memoryless randomizer: it maximizes variation, unpredictability, and increases challenge.
- OskarS 7y agoYou speak of Tetris like it's some abstract concept that has always existed and is like some eternal truth. It's not: it's a game that was created by one man, Alexey Pajitnov, who also just happen to be "the current IP holder" you dismiss so easily (he's the co-owner of the Tetris Company). So no, it's not an "abstract game with endless possibilities". Tetris is Tetris, a concrete game made by a great game designer. It gets cloned and copied constantly, so much so that people forget that it was actually authored by a person. Comments like yours erase Pajitnov's authorship and ownership, which he has rightly earned. Nobody talks of any other kind of media this way, we shouldn't do it about games either. When speaking of Harry Potter, no one thinks of JK Rowling as just "the current IP holder" (she's the author!), and no one considers fan fiction on the same level as the real thing. The article states "when the game plays, the tetrominos are selected at random". When it comes to standard Tetris, as defined by the Tetris Company, this is incorrect. I was correcting the error. Obviously you can do it however you want, but that is how Tetris does it. As for what's more fun, obviously opinions can differ. But as someone who plays a lot (A LOT) of Tetris, I personally vastly prefer the 7-bag system. It really opens up the game strategically, and it allows you to play much faster.
- defertoreptar 7y agoI agree that credit is due to the wonderfully creative game designer, Mr. Pajitnov. I don't see this as contradictory to my point. > So no, it's not an "abstract game with endless possibilities". Tetris is Tetris, a concrete game While protecting their IP in a court of law, The Tetris Company successfully argued that it does not exhibit scènes à faire because it is a purely abstract game. > Moreover, Xio does not dispute that Tetris is a purely fanciful game, meaning it has no grounding in the real world, unlike a video game simulating a karate match or a golf game. Therefore, the analyses in Data East and Incredible Technologies are largely inapplicable; the scènes à faire doctrine has little weight in instances such as this because there are no expressive elements “standard, stock, or common” to a unique puzzle game that is divorced from any real world representation. > https://law.justia.com/cases/federal/district-courts/new-jersey/njdce/3:2009cv06115/235418/61/ https://law.justia.com/cases/federal/district-courts/new-jer...
- 60654 7y agoThe third one looks like a LFSR [0]. I didn't recognize it at first because the factors used here are not the common ones that produce well known periods; not sure what period this one would have. For what it's worth, LFSRs can be nice for generating random sequences with periods of predictable length - and they're cheap! They also recently got a bit of a publicity bump with the rediscovery of how Doom used one for the "death fade" transition, that randomly but exhaustively overpainted all screen pixels with blood red. [2] [0] https://en.m.wikipedia.org/wiki/Linear-feedback_shift_register https://en.m.wikipedia.org/wiki/Linear-feedback_shift_regist... [1] http://fabiensanglard.net/fizzlefade/index.php http://fabiensanglard.net/fizzlefade/index.php But in general - yeah, there's a lot to consider with picking RNGs, not the least of which being context. For games Xorshift is often a good enough choice because it's fast, small, and the game environment is chaotic already (because of the unpredictable players) so there is rarely need for something much stronger.
- OskarS 7y agoFair enough, I was perhaps a bit dismissive of that one and should have looked closer. LFSR can certainly be perfectly acceptible PRNGs for this kind of game. By the way, Doom's random number generator for the game mechanics... not so fantastic [0] :) [0] https://github.com/id-Software/DOOM/blob/master/linuxdoom-1.10/m_random.c https://github.com/id-Software/DOOM/blob/master/linuxdoom-1....
- 60654 7y agoOh hah, that's a bit unexpected. Ahh the crazy days of 386SX desktops without math coprocessors :)
- remysharp 7y agoHey there \o. original author of said hubba article :) Just wanted to offer some missing bits you might find interesting, specifically, the third function you don't recognise is the random function from the original Tetris on the NES. It comes from the article[0] that went through all the asm of the NES Tetris cartridge. And yup, that first one is quite quite terrible for random - it came from some of the first results in StackOverflow intentionally looking for something that was overly simple and "bad" at random. But really the article was about testing and visualising how your own random functions might be. [0]: https://meatfighter.com/nintendotetrisai/#Picking_Tetriminos https://meatfighter.com/nintendotetrisai/#Picking_Tetriminos