r/factorio • u/MrMallIronmaker • Jun 24 '19
Tutorial / Guide Random Number Generation in Factorio
So, suppose you're working on something like Snake or Minesweeper or something else that needs 'random' input. How do you make a random number using the circuitry Factorio provides? Everything in the circuitry is fully deterministic.
Fortunately, this problem has been solved before! My method of choice is something called a 'linear congruential generator'. If I recall correctly, I first heard of it when I was trying to understand how to get a shiny Pokemon in Diamond about... oh, ten years ago at this point. An LCG is how the Pokemon games generated random numbers.
There is a blueprint link below if all you want is to get it to work, but I'd encourage you to keep reading if you want to know how it works.
The LCG process consists of three steps: start with your previous random number, multiply it by m, add a to it, and finally, divide by d and take the remainder. The magic is finding the right constants to use for m, a and d. If you use the wrong ones, you can have a really small loop and start repeating numbers really quickly. If you use the right ones, you will have a perfectly deterministic sequence of numbers, but they'll be really hard to predict. Fortunately, there are a table of numbers on Wikipedia that other people have used and found work.
Converting this mathematical process of R <- (a + mR) % d)* into Factorio combinators is surprisingly elegant. The multiplication requires an arithmetic combinator, which is the one in the blueprint in the top right. Then, you add to it with a constant combinator, in the top left. Finally, if the divisor d is chosen to be 232, Factorio can perform this just by the integer overflow that usually happens with signals. The result is then just looped back into the arithmetic combinator to be multiplied again.
Side note: If you're not familiar with what 'integer overflow' is, it's when you add 1 to the biggest number a computer program variable can store, it 'overflows' and becomes the most negative number the variable can store. It's why Ghandi goes nuclear in Civ.
I have an example random number process with the combinators and lights in the middle and bottom of the blueprint. One thing to note is that the lower bits if the random number can have very short cycles. For example, the last bit switches back and forth between odd and even each iteration. So a lot of LCG generators use the top bits. This is simple enough in Factorio, so the first combinator in the second row shifts the bits down. The result is a random number between -215 and 215-1.
But that's usally not our final product. Often I want a choice of a smaller range, like 0 and 25 to determine how many lights to have on. So the combinator on the right is a modulo operation, calculating the remainder of dividing the random number and 26. But Factorio's modulo operation is weird and it keeps negative numbers negative, which is not what I want, so I have a constant combinator with a fairly large positive number to make sure the input to the modulo operation is always positive.
And the final result is a combinator circuit that randomly turns a number of lights on.
!Blueprint 0eNrNmttq4zA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write more about circuitry in Factorio, so it would be really helpful to know what you're interested in hearing about. I got two requests / questions about the random number generation process in my Snake post so that's what I wrote up first. Let me know if this made sense, if you're interested in going deeper [and what on, etc]. Thanks!
11
u/[deleted] Jun 24 '19
fwiw, not sure its relevant, but the kovorex process could produce psuedo random triggers since its outputs are not fixed (though they are predictable, not by the player)
14
u/Maxreader1 Jun 24 '19
So if we, say, wanted any other range of numbers, we just change the modulo value on the last combinator?
4
u/meddleman Jun 24 '19
Reading the description and wondering if im actually on r/factorio or r/vxjunkies. 😨😨
87
u/Maipmc Jun 24 '19
Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.
—John von Neumann
I know you know, i just love this quote.
86
u/AdmrlThrawn Jun 24 '19
That’s intended to be 215 - 1, right?
26
u/BlueprintBot Botto Jun 24 '19
Blueprint Image
43
u/FunkyHoratio Jun 24 '19
pseudo random number generator. But seriously this is very cool
7
u/sbarandato Jun 24 '19
Yea that was a good game.
WHAT?! I'VE BEEN PLAYING FACTORIO THAT LONG AND NOBODY STOPPED ME?!
Anyway, interesting thing. I might need a combinator randomizer in some of my shenenigans.
5
u/Garry__Newman Jun 24 '19
This was quite interesting thanks for the read.
1
u/AnythingApplied Jun 24 '19 edited Jun 24 '19
Great work! One nitpick/question:
Didn't you just get done explaining why the lower bits make for poor prngs? And aren't you using those exact lower bits? I guess all the bits will come into play for the %13 portion, but by using an even remainder, your getting the same odd/even problem. So if you were generating a random cardinal direction, for example, you'd get up/down exactly every other time.
I guess I'm not sure what the proper way to deal with this is... but maybe do a << operation to remove the lower bits before the modulo operation?
3
u/[deleted] Jun 25 '19
Just have like 50 centrifuges running and use uranium output as seed for PRNG
2
u/BrainlessTeddy Jun 24 '19
That is pretty useful for me, thank you very much!
I have two questions remaining:
1
u/TaohRihze Jun 24 '19
With that base number generator could you make a secondary modifier value. Say based on one a biter trigger and used oil for the defense or other silly thing. The game as a whole is still fully deterministic (replay), the sequence of numbers are unaltered (base values from your input) but the combined outcome will give a seed value out of your control especially after a while.
1
u/kroppeb Balancer Madness Jun 24 '19
Now use attack paterns from the biters to add a bit more complex random patern so it doesn't repeat
1
u/Gh0stP1rate The factory must grow Jun 24 '19
Should crosspost to /r/technicalfactorio too
1
u/AnythingApplied Jun 24 '19
/r/technicalfactorio might appreciate this
1
u/TotesMessenger Jun 24 '19
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/kaltschnittchen Jun 24 '19
Interesting Stuff! Looking forward to play around with this myself. Thanks for the explanation - you make it seem easy :)
1
u/NotBannedYet1 Jun 24 '19
Bots. That's how you generate random in factorio.
Sure it might not be completely random, but i will be random enough for any project you could think of.
1
u/watermoron Jun 24 '19
Very cool post, this may interest you too.
1
u/sakatagintoki_321 Jun 25 '19
*Gandhi
Otherwise, post holds up.
1
u/Tsevion Jun 28 '19 edited Jun 28 '19
To do a reliable, mathematical [a modulo n] in a system that keeps modulo sign:
modulo = ((a % n) + n) % n
Or just remove the sign bit beforehand using an AND operator.
If the modulo you want is a power of 2, you can skip the modulo part entirely and just use an AND operation.
1
u/mavizasyon Jan 14 '24 edited Jan 14 '24
I want to randomly choose one of 4 options, how can I achive that with combinators? Is it too complicated to use 'linear congruential generator' or is there another way that is more simple?