r/factorio
•
u/Gegellibu
logic noodles
•
Sep 12 '26
•
1
14
2
1
Design / Blueprint Tired of gambling for quality? Try math instead!
Do YOU have a gambling problem? Does your quality roller seem to never give you that bump in quality that you so rightfully deserve? Well worry no more, with just a bit of math you too can guarantee** legendaries in no time. Just follow this simple build and you too may soon be clad in full legendary gear.
** mostly guarantee
In all seriousness though: Hi everyone, today's the day I finally showcase a project I've worked on, on and off, for almost three years by now. As you can see in the video above, I built a machine which allows me to ingest common quality items into a craft and instantly upgrade to legendary outputs.
How?
As you may know, in games there is the concept of RNGs (or random number generators) underpinning randomly occurring events. But what if I told you that these random numbers... are not actually random (shocker for a deterministic game I know)!
Instead they are generated by a fixed algorithm that produces a sequence which merely seems random. If you start from the same initial state, then you will observe the exact same sequence!
What this means is, that once we figure out the position that the game's RNG currently is at within this sequence, we may as well start performing the same computations that the game will do for future random numbers, but get there before the game does. This allows us to "look ahead" in the sequence and predict future """random""" outcomes, even before they occur.
What's even more interesting is that this computation is actually quite cheap, which allows one to reimplement it entirely within combinators!
This led me to build this machine, which can (1) sample and deduce from the observations the game's current RNG state, (2) predict many future outputs of calls to the random generator, (3) automatically skip towards lucrative outcomes and (4) craft the desired item at the right time, to hit the rare instances where an item immediately upgrades from common to legendary quality.
And the most amazing part is that this is actually sort-of feasible in game! As in it does work, but only in a semi-controlled environment. Factorio shares the same random generator with a bunch of other parts of the game, which may desync our current guess of the game state, which if not handled leads us back to basically blindly guessing.For example, Fulgora lightning strike positioning is determined by the same generator used for crafting result creation and quality rolls.
Lastly, as I already said, this project has been going on since about 3 years (right around when quality was first announced in FFF#375). In the mean time, others have independently shared their findings: kovaxis in the factorio forums and d4s_over_dt4 in the official discord, but as far as I know this fully autonomous machine is (still) the first of its kind.
There is sadly one major drawback. Since I waited so long to release this the devs have made strides on the 2.1. update. Within it, the way the RNG is called also got a rework, which broke some of the assumptions I made while designing this contraption (mainly about how many calls a single craft makes) breaking it in 2.1, so it is currently limited only to 2.0.
I figure some of you might enjoy a deeper dive, for which you can have a look at my full write up (which was part of the reason I didn't show this off for so long):
https://gegell.github.io/posts/factorio-rng/
It contains all the juicy technical details of basically every minute part of the machine, covering the underlying mathematics (with interactive visualizations!), in game setup, containing a world download, blueprint string and the code used for generating parts of the machine and a deeper discussion into why it may break down.
82
u/Lachtheblock Sep 12 '26 edited Sep 12 '26
This is very cool. I have never thought about RNG manip in game, but makes sense.
I assume (in 2.0 atleast) that a lot of things would be driven by the same RNG? Asteroids? Biter expansion? Of course it also means you can only be crafting a single quality component at a time too?
Is biter behavior also controlled in this way?
I could see this legimately being useful in some sort of biters off sushi challenge run.
Absolutely will be checking out your deep dive.
16
u/Courmisch Sep 12 '26
If we have Quality without Space Age, and no other quality module anywhere, are there still perturbations from other RNG consumers?
10
u/QuizardNr7 Sep 12 '26
"I got this idea from my real work as high frequency trader" - OP, allegedly
5
u/Ozryela Sep 12 '26 edited Sep 12 '26
I understand the idea behind this, but I don't understand how you can reliably isolate a single random event. Surely there's lots of random events constantly going on in the background. You already mentioned Fulgora lightning, but there's also asteroids, biters, even fish. Oh uranium processing too. I'm sure there's others I'm not even thinking about right now.
So I guessing this only works if it's day on Fulgora (or you don't have the planet yet), if you have no spaceships and if you have eliminated all biters and fish on Nauvis? That's some rather harsh pre-conditions.
edit It's also rather interesting that this only works because the Factorio devs decided to use a custom RNG algorithm (taus88) with a state of only 96 bits. The game is written in C++, which by default uses Mersenne Twister for random number generation. Mersenne Twister has a state of 19968 bits, so you'd need a lot of combinators to keep track of its state compared to the one that is actually used in the game.
I wonder why they chose a different algorithm. Speed, presumably. But that is strange because you'd think the game doesn't use enough random numbers to justify that.
4
u/tru_mu_ choo choo Sep 12 '26
Do the types and qualities of the modules affect the calculations? Could you have this work with just one q1 quality 1 module in the legendary assembler? And do the modules of the other assemblers affect the quality of the legendary one too?
4
u/Dtitan Sep 12 '26
My friend … I read the write up … holy smokes that’s nuts.
How long did implementing this take?
1.7k
u/MightyBigMinus Sep 12 '26
new intended way dropped
1.4k
u/Korporal_kagger Sep 12 '26
Any sufficiently evolved factory is indistinguishable from magic
826
u/Cephell Sep 12 '26
Space casinos died for this
2
u/Lardbucket_ Sep 12 '26 edited Sep 12 '26
Is there a plan to update it once 2.1 becomes stable with the RNG changes you mentioned in the blog?
Had stumbled upon the forum thread you mentioned and had been tinkering a month or two ago doing the maths myself with recycling of gears but could never get it to work so gave up, happy to hear it sounds like the RNG calculations have changed and not just me being an idiot
2
u/MaleficentCow8513 Sep 12 '26
You can also replicate the sequence if you know the seed. Is there no way to access the seed from game files?
2
u/Rsccman Sep 12 '26
Blueprint please?
2
u/TonicAndDjinn Sep 13 '26
Have you thought about using random input on selector combinators? I think you could read the seed much more quickly with one selector combinator shuffling two input signals once per tick; likewise you could advance the PRNG quickly by having blocks of selector coordinators on power networks controlled by switches which get selectively turned on for one tick.
248
u/Agador777 Sep 12 '26
Wow! This is some woodoo magic shit here! Outstanding!
80
u/Overwatcher_Leo Sep 12 '26 •
Rng manipulation? In my casino game?
75
u/Stunning_Box8782 Sep 12 '26
This is like looking at a single particle and determining the state of the entire universe
276
u/Correctsmorons69 Sep 12 '26
Creepy-pasta version: it works in real life too.
139
u/foxgirlmoon Sep 12 '26
Pure insanity.
59
u/deco1000 Sep 12 '26
This is absolutely incredible, I have absolutely no idea how you could reverse-engineer the random number generator for the game and implement this kind of thing, but it feels amazing that it is actually possible to do. Kudos to you! This game and this community are amazing
112
u/Greningas Sep 12 '26
What in Laplace's demon is this.
91
u/what_the_fuck_clown ÐŌ ʼnºŤ ĿĖŢ ŦĤĖ ĘŸĚŜ ØF £ŲŁĜºЯÄ ŦŒ ŞŢÅŖĖ ŀʼnŤŎ ŶŒÙŔ ŠºÛĽ Sep 12 '26
your insanity inspires and madness brings hope.
26
u/clif08 Sep 12 '26
One of the craziest things I've ever seen on this sub.
21
u/LeifDTO You haven't automated math yet? Sep 12 '26
Only 5 combinators? How did y- HOLY MOTHER OF GOD
18
u/Runelt99 Sep 12 '26
Bro hacked bitcoin mining
16
u/Funintuitive Sep 12 '26
That's pretty cool. Congrats on the achievement.
15
u/mist_kaefer Sep 12 '26
That zoom out was legendary
13
u/Aggressive_South_991 Sep 12 '26
i just use spaghetti and figure it out as i go... and then theres people that do this.
Factorio is probably the game with the biggest (gigantic) gap between casuals and actual good players. holy shit.
13
u/DiabolicallyOrange Sep 12 '26
What. The. Fuck.
12
u/Grismor2 Sep 12 '26
"He who meddles with the threads of fate should prepare himself for the wrath of Atropos."
—Me
22
u/MakingAngels Sep 12 '26
The absolute mad lads (and ladies) of this community never stop amazing me. This game is bonkers with its ability to enable such creativity.
10
u/tiogshi very picky Sep 12 '26
Guaranteed results every time; all you gotta do is consult Maxwell's Demon.
9
u/Old_Ad9574 Sep 13 '26
Bro is predicting the future with math, and I'm here, struggling to set up a timer
8
u/DidYouShartInMyPants Sep 12 '26
WITCH! 🫵🫵🔥🔥
2
u/weeknie Sep 12 '26
Amazing work. One question though: in this post and in your write up, you seem to make the connection that the RNG is implemented as pseudo RNG to ensure the game remains deterministic. It is something that is important for Factorio, since it allows all clients in multi-player to make the exact same calculations even if "randomness" is required to be involved.
However, afaik there is no true way to do randomness in a computer, barring specialized hardware maybe. The way randomness is implemented is just the way "randomness" works in computers. The determinism, given enough knowledge, is a side effect, not the main purpose.
5
u/Technical-Ad9571 Express engineer Sep 12 '26
This is just SOO cool!
I knew something like this was possible, but goddamn this is just PEAK
I kinda got the process until you got to Galois Field, which make me short circuit. I did get the programming part tho. Great blog post!
5
u/Simic13 Sep 12 '26
Wow, this is the level of diving into the game...
You should not be allowed to visit casino ...
4
u/SevereBruhMoments Disco Lab! Sep 12 '26
Very interesting
4
u/Helpmefromthememes Sep 12 '26
Someone call Michael Hendricks, his Kwality setup might benefit from an upgrade !
4
u/Raynsen Sep 12 '26
Bro, when was the last time you saw sunlight?
4
u/Brewer_Lex Sep 12 '26
I read your blog post about it and it was an impressive academic write up if I’ve ever seen one. Your process and problem solving was beautiful.
4
u/lysianth Sep 12 '26
holy shit,
have you considered cyber security?
This is way more impressive than the comments are giving you credit for.
1
u/dmigowski Sep 12 '26
Does this work if you have two of them?
1
u/mglachrome Sep 12 '26
wait, you can color concrete?
1
u/sypwn Sep 12 '26
And here I thought I was so clever to use automated recipe cycling to sink excess liquids on Aquilo. This is a level of game mechanics abuse so far above that I can't even. Bravo.
Quick question because I can only skim the blog post right now: for the process of determining the current state (using the 44x recyclers) and feeding that seed into the predictor, is it fully automated as well? Or does it require the player to do some frame-by-frame inputs to set things up at first?
1
u/Winter_Ad6784 Sep 12 '26
does this work with other upcyclers running? can you run multiple of these at once?
1
u/VoidRaizer Sep 13 '26
Fascinating. I also couldn't help but notice it looks like your blog is exactly what I was just looking into setting up for my wife - I'm guessing you used Obsidian + github pages to host your site? Probably Vercel too? Any advice you can offer regarding this for someone just starting to look into it?
3
u/and0p Sep 12 '26
Insanely cool.
3
u/MelangeBot Sep 12 '26
That write up is amazing, had to give up half way cause I am just not mathemamically inclined enought but regardless it was super intressting. I still don't get how you can use ingame combinators to figure out where in the sequence the RNG is. But that's because I gave up on reading it. I might return when I feel smarter.
3
u/prash3r Speghetti Sep 12 '26
This is unironically the quantum physics research of the factorio universe.
Analyzing and then predicting and steering the probabilities of underlIng randomness events. Man I hope this never works in our universe.
3
u/RockRancher24 Sep 12 '26
We fucking bending reality and mocking the Gods out here
3
u/Dummy1707 Sep 12 '26
This goes in my hall of fame of Factorio players who inspire-but-frighten me.
Between the guy who recoded the game but with multithreading for his Py save, and the train-based signal-less megabase.
3
u/xTMagTx Sep 13 '26
If this were Vegas they'd put you a list.
3
u/Yggdrazzil Sep 13 '26
That was heartbreaking to read.
I tried reading your documentation on Github but quickly realised my understanding of math was insufficient. I really liked the formatting of the blog though. The parts that I did understand were pleasant to read.
3
u/maceo107 Sep 14 '26
I can't stop thinking about this since I read it.
2
u/ArtieTheFashionDemon Sep 12 '26
Mr. Jackpots! Mr Jackpots!
2
u/Terrorscream Sep 12 '26
Exploiting the RNG seed is brilliant
2
u/DaMonkfish < a purple penis Sep 12 '26
The RNG knows where it is at all time because it knows where it isn't.
2
u/BibianaAudris Sep 12 '26
Nice work! I've been interested in this project too but never managed your dedication. Your designs are so much more clever than what I have thought of.
My thought on the output side: for a "practical" run in a real save, my conclusions were that the most "valuable" target is manipulating the recycling productivity (and maybe quality) to 100% for recycle-into-self things, like science bottles and planet-specific raw materials. It can utilize 1/4 or 1/16 RNG calls so:
2
u/ex0planetary Sep 12 '26
I'd dare say Wube should separate the RNG and make this an intended mechanic because it feels very much in the spirit of Factorio lmao
2
u/W1nn4rMrTTxx Sep 12 '26
HERETECH
2
u/eisenefaust Sep 12 '26
Cool! Another independently derived version of this I saw back in May: https://www.youtube.com/watch?v=ZXwIYKzrkd8
2
u/AlbeniChocolate Sep 12 '26
only in 2.0 bummmmmmer i was getting excited. amazing concept though awesome work man
2
u/sharktail_tanker Sep 12 '26
Please man keep working on it, this is the only way I'd ever turn quality on in my games
2
u/sobrique Sep 12 '26
This is also a beautiful example of why good quality random is so important to encryption and security.
A password with a "time" seed and a 30 day expiry is a lot easier to "figure out" than "true random" might suggest.
I really love what you have done. I think you might be slightly mad, but it's genuinely delightful as an example of why "good" random is needed for security applications (like encryption)
2
u/leglesslegolegolas Sep 12 '26
This reminds me of the Infinite Improbability Drive from Hitchhiker's Guide to the Galaxy...
2
u/VeniABE Sep 13 '26
This cannot ever have enough upvotes.
2
u/QThellimist Sep 13 '26
One of the best posts in the whole subreddit 👏
2
u/Beneficial-Point9142 Sep 13 '26
Sick
2
u/tevelee Sep 13 '26
Well, it’s already broken by factorio 2.1
2
u/Fit_Giraffe_748 Sep 13 '26
haha yeah i totally understand this and am not confused at all
2
u/_Sanchous Sep 13 '26
Math players are goated👍
2
u/lukeybue Sep 13 '26
I feel, this post qualifies to be officially be marked "black magic". The last one on this forum was https://www.reddit.com/r/factorio/comments/6hi0ac/black_magic_2_belt_full_throughput_splitter_sorter/, which is comparable in witchcraft and sorcery.
2
u/Due-Chance-8540 Sep 14 '26
I fucking knew someone will figure out and reverse engineer the prng. congrats.
2
u/Litoprobka Sep 24 '26
RNGesus ex machina
2
u/Alpha1Actual Sep 12 '26
I’ll be honest, I only superficially understand what you’ve done but amazing work! This is the kind of thing I love the Factorio community for
2
u/alive1 Sep 13 '26
This right here is the pure, distilled, genius mastermind core of what makes this game so freaking amazing. You have tapped into something otherworldly. Divine. Absolute game mastery. Bravo.
1
u/oobanooba- The Factory Must Groove Sep 12 '26
That’s pretty clever, thanks op
1
u/Aesthetically Rushing all 3 planets simultaneously next playthrough Sep 12 '26
Legend.
1
u/Drfoxthefurry Sep 12 '26
trading randomness with speed sounds like a good trade off
1
u/miketastic_art Sep 12 '26
there's like, too many numbers.
1
u/2DHypercube Constructor of worlds Sep 12 '26
This is amazing and hilarious!
1
u/sobrique Sep 12 '26
This is terrifying dark magic and I love it.
1
u/unhott Sep 12 '26
amazing
1
u/cybertruckboat Sep 12 '26
Unbelievable!! 😍
1
u/constant-runtime Sep 12 '26
this is absolutely amazing! I would love a 10-minute long video explaining how RNG manipulation in factorio please.
1
u/solonit WE BRAKE FOR NOBODY Sep 12 '26
If this was Hollywood, OP would be immediately recruited as The Hacker Man in either a top-secret black book mission by gov; or a heist. Probably both.
Wait that's just Ocean 13
1
u/jbroombroom Sep 12 '26
This is insane.
1
u/OrduluPro52 Sep 12 '26
damn
1
u/MartinMystikJonas Sep 12 '26
This is the coolest, most insane and truly admirable piece of engineering I ever saw.
1
u/RyanSpunk Sep 12 '26
Peak Factorio
1
u/Evening_Archer_2202 Sep 12 '26
Congratulations!!!!
1
u/MrFizzyBubbs Sep 12 '26
This is sick
1
u/geneticfreak6 Sep 12 '26
Doesn't seem like it's within the spirit of the game. Obviously there's no such thing as random in this universe but it seems to me that it seems to be good enough
1
u/VeryGoldGolden Sep 12 '26
Soon people will defend their PhD thesis on Factorio.
1
u/IncredibleReferencer Sep 12 '26
As someone who absolutely hates the quality mod (don't come at me I'm glad ya'll like it its just not for me), this is so interesting I'm going to try it out.
1
u/Jester185 Sep 12 '26
They use the dark magic, we have a witch!
1
u/MattieShoes Sep 12 '26
Man, I love the factorio community :-D This is... legendary. heh.
1
u/bugprof2020 Sep 12 '26
Guass's dream of a world modeled by statistics has become a nightmare.
1
u/lordwafflesbane Sep 12 '26
mom come pick me up I'm scared they're doing black magic.
1
u/A_man_from_america Sep 12 '26
LETS GO GAMBLING!
1
u/pertur4bo Sep 12 '26
This is incredible.
1
u/jmpaul320 Sep 12 '26
This guy literally found a way to see the future
1
u/Alian713 Sep 12 '26
Reversing and forcing pRNG state to force quality outcomes... absolute genius XD
And the funny thing is, due to the ridiculous number of times the rng might be rolled in the game, they can't simply use a cryptographically secure pRNG or that kills perf. How would you even attempt to patch this as a dev 😂
1
u/Specialist_Ice_1838 Sep 12 '26
Where Is the fun?
1
u/Inner-Ad-9478 Sep 12 '26
Still sane, engineer?
1
u/Then_Entertainment97 Sep 12 '26
We walk amongst oracles.
1
u/Panzerv2003 Sep 12 '26
Out with space casinos in with... whatever the fuck is this monstrosity. Great job engineer!
1
u/Head-Objective-7480 Sep 12 '26
Its glorious. I also play 2.0 so its perfect for me lmao, I have thought about upgrading to 2.1 for the logic and stuff, but i also dont want anything to bug my save of like 150 hours lol
1
u/Shiranui987 Sep 12 '26
Man this is amazing! I have to give my salute and congratulations to your work and research, but also to the devs, Its incredible that they could achieve such a turing perfect machine that we could analyze it and use it from within it lol
1
u/timonix Sep 12 '26
Oh.. ok... oh.... This is cursed
1
u/jsrobson10 Sep 12 '26
gambler: "trust me, i have a system"
1
u/maceo107 Sep 12 '26
WOW! Hope you can get it updated for the latest version.
1
u/StickyDeltaStrike Sep 12 '26
Mind blown … amazing
1
u/eric23456 Sep 12 '26
Awesome. If the devs want to fix it, they need to do a crypto-rng. Probably the fastest option would be to do a stream cipher using aes and generate a true-random seed for each game. People could reverse-engineer it by extracting it from the save game file, but pulling it in-game would be impossible.
1
u/ffsollie Sep 12 '26
We should name this around this posters name,
Maybe gegells giggle
1
u/Lovestick Sep 12 '26
This game keeps calling.
1
u/3davideo Doesn't use Flame Turrets Sep 13 '26
Neat, but even though I've never actually gotten around to quality during any of my playthroughs, I know the practical solution to quality isn't gambling, but quantity. If you need a d6 to roll a 1, you just need to roll lots and lots of d6s instead of worrying too much about individual rolls.
1
u/infam0usx Sep 13 '26
Oh god, doing stuff like that just using game mechanics is pure madness. Well done and mad respect!
1
u/Exciting-Carpet3873 Sep 13 '26
I refuse this amazing contraption and choose to remain stoopid. Thank you, but no thank you, SIR! If that is even your name!
1
u/Numerous_Schedule896 Sep 13 '26
Isn't this the same operating principle for rng they used to catch dream cheating rods in minecraft
1
u/paphnutius Sep 13 '26
"We'd like to invite you to play any other game in the casino, but your quality crafting days are over here."
1
u/lunat1cakos Sep 13 '26
i felt pain .
1
u/Rakomi Sep 13 '26
This is very impressive!!! I was worried you broke the system and that we'd now see this blueprint spammed in every competitive game...
1
u/Choice-Comment-1864 Sep 13 '26
Friends, is it possible to use a transport drone to deliver coal to chests or to a specific building? Thanks for the help.
1
u/authilmorros Sep 13 '26
I remember in minecraft the people from scicraft doing something similar, manipulating random events to allign mob spawns to a specific position
1
u/OtlichnikTop-Off Sep 13 '26
oh yea RNG manipulation my favorite
1
u/sabrewolfACS Sep 13 '26
fastinating!
could this be parallelised? meaning : at the same tick feed 100 different fabrications? and would it be easy to allow higher frequency with two step quality jumps (e.g. A) smelters/miners for rare iron and assemblers for rare wires. then B) assembler for highest circuits?
1
u/FoxStarSnake Sep 14 '26
ITS WIZARD TIME MOTHER FUCKER
1
u/Thankedsphere Sep 15 '26
mario maker 2 levels of technology
1
u/Maistho Sep 16 '26
This is really cool!
I saw another example of reverse engineering the RNG in a video game a few years ago, where speedrunners made a tool to solve a terrible minigame in windwaker
https://www.youtube.com/watch?v=1hs451PfFzQ
https://github.com/petersn/web-sploosh-kaboom
1
u/choicetomake Sep 16 '26
Factorio card counting.
1
u/Crystalcompany Sep 16 '26
Let's go gambling
1
u/daddystrongdick Sep 16 '26
Sick
1
u/mad_about_this Sep 24 '26
Wow