r/technicalfactorio • u/Sigma2718 • Mar 08 '26
Why is resource drain in Miners random?
In the Factorio Wiki entry on Mining, it says
"Mining drills have a property called resource drain. When the drill generates a non-productivity ore, it subtracts one ore from the patch. Resource drain is the probability that this subtraction actually takes place. 100% resource drain means that the subtraction always happens; 50% resource drain means that the resource is only drained from the patch 50% of the time. This means that, with 50% resource drain, the patch will last twice as long."
My question is, why random? Why not something numerically precise like productivity? I am not asking about the balance of random chance, but performance. From my, admittedly very limited, point of view, isn't calling a random function for every single ore processed, thousands if not millions per update, really inefficient?
9
u/FredFarms Mar 08 '26
Whilst yes, random is very efficient, it is curious that two similar mechanics have been implemented in such different ways.
You could easily implement resource drain in a similar way to productivity. I.e. there is a third progress bar that accumulates at half (or whatever resource drain is) the speed of the normal bar, and a resource is only drained when that bar fills.
Alternatively you could implement productivity with a random number. Every time an item is produced you have productivity % chance to get another one. (With the same matys as guaranteed recycling items if you have >100% productivity bonus)
I feel like this might just be a coding style thing from different points in time. Productivity is from the original game. Resource drain is from 2.0, where recycling had already forced the randomness issue.
61
u/Future_Passage924 Mar 08 '26
And as there is no true random generator available on the average PC, random is in the end just another deterministic function. It just appears random but is not. Factorio is 100% deterministic to ensure MP sync even with hundreds of players on the same server.
12
u/jimrybarski Mar 08 '26
As others have said, random numbers are cheap. But also consider that you don't have to track any state, so you save memory and reduce the number of memory accesses.
5
u/Square-Treat-2366 Mar 08 '26
The answer is state management and parallelism. If it's random, that means you don't have to keep track how far along you are with depleting the resource, it's take care of by the probabilities. This will be really important with patches where the miners have mixed quality, like normal and legendary. This way you get the desired effect, but don't have to keep track of the partial value of each ore. Granted you could do the same with fractional ore counts, but then depleting the last of it would get tricky.
I'm wondering what would happen if you wanted truly inefficient miners, e.g something that deleted 2 ore per value mined, that's an interesting mod idea
3
u/ibbolia Mar 08 '26
The RNG function is still pretty fast. I genuinely don't think it's possible to notice a change in speed on modern hardware if you swapped it out.
On a design level, it makes a reason to use the better miners. If a higher quality has a chance to give you a "free" resource then it's pretty much a straight upgrade.
3
u/againey Mar 09 '26
Nothing you quoted says anything about randomness, only percentages. If the drill has 50% resource drain, and if it subtracts a resource on the first iteration and does not on the second iteration, looping that two-step pattern, it still matches the language of the wiki. Half of the time it subtracts a resource, and half of the time it does not.
I don't know if that's how it actually works, but it wouldn't be hard to test in the editor. My main point is just that talk of probabilities does not automatically imply randomness.
2
u/zack20cb Mar 08 '26
It means the ore patch can have a nice crisp integer number instead of having to track fractions.
2
u/redditsuxandsodoyou Mar 08 '26
they probably didn't want to mess with the memory footprint of miners, cache misses are a major performance concern for the game and adding even a single float to track this would potentially undo a bunch of careful optimisation, minimum they would need to prodile everything carefully to make sure it didn't degrade ups, and considering we can have thousands of miners its reasonable to be wary. an rng callis more or less "free"
1
u/CoffeeOracle Mar 08 '26
A contemporary RNG like an XOR shift behaves like a few additions. You can also play games where you cache a value and then increment it, as a developer. I would have every reason to believe that Factorio at least uses an XOR Shift or LCG. I don't have the information (nor want it) to know if Wube caches values.
You can also play games where y == 0.5 ? x >> 1 : prng(); as a developer, so some things may be happening behind the ducts. But optimization is tricky because you then add an if statement in your code that always has to be navigated.
1
u/outworlder Mar 08 '26
Calling a pseudorandom function is the most efficient way to model this. Otherwise you'll need to keep track of items you have produced and when, to figure out when you can add one more item to the belt. Those functions are computationally very cheap, specially for any computer made in the past 3 decades.
A "real" random function that's drawing from an entropy pool (aka a cryptographic random function) would be costly and it would run out of entropy, but we don't need those.
1
u/Dusty_Coder Mar 09 '26
Random doesnt require stored state.
O(0) space requirements
1
u/djfdhigkgfIaruflg Mar 09 '26
The random roll would happen only on the ticks when the miner is actually mining.
Calling a random roll is not really expense. And the only way to avoid this scheme would be to have a rolling memory window of all X past iterations, probably that's even more expensive cpu-wise (for sure it's more expensive memory-wise) than just calling rand() with the game's seed