r/computerscience • • Oct 25 '26

Discussion Is simulation a function?

Question to settle a debate which I recently had with two of my colleagues.

One of my colleages argued all simulations are mathematically functions by definitions, since for every set of arguments they need to return one outcome. As an example we took a cellular automaton simulating a forest fire. They stated that everytime you give it a particular starting state, the end state will be the same, but I don't agree with that. To simulate humidity or different wind speeds and directions one might use an rng, so the same start map run twice might give 2 different results, which means it's mathematically not a function. My friend countered that because of how rngs work, with the same seed the will always be the same. You don't need to set a seed tho and I can imagine cases when it's even by design - for example when you can't be sure which direction the wind will be coming from and you want your simulation to account for any of them.

But here's the bit that we couldn't agree upon. My friend said, that even without seed, or using quantum randomness, that randomness is always based on something and as such must be treated as kind of a baked in argument. Because of that, if you manage to somehow recreate the exact same "randomness state", the outcome will never change.

From my point of view that's not the case. Even using simple time based pseudorandom number generators, to you as someone analysing such simulation, that rng should not be treated as argument. The way I see it it's just a black box, that "magically" gives you a number, you should treat it as truly random and use it to approximate all the different variables, that are too complicated to calculate each and every time you run the simulation or are just out of scope. Therefore the same input can give you different outputs, which makes the simulation itself not a function by mathematical definition.

What are your opinions on this?

0 Upvotes

41 comments sorted by

View all comments

1

u/dnabre Oct 25 '26

Ok, bunch of idea floating around here. I'm going to take "simulations" as being basically any kind of program here, with the possible limitation that it's not interactive - a program reading input from somewhere, runs, and outputs to somewhere.

The property your colleague is describing is called referential transparency. Very briefly, if you call a function with the same input, you always get the same output. This is an extremely useful property for optimization and reasoning about programs (two very intermingled things in practice).

In CS, we have the term 'pure function'. It's a function that has referential transparency and has no side effects. By side effect, we are talking about changing anything anywhere. Modifying the value of a variable (one inside the function that are reset the next time you run it don't count), making other code run that would have side effects, or any kind of I/O. These 'pure functions' are basically the same a mathematical function. Side note for thought, can a pure function crash?

They have a lot of useful properties. Since they will always give the same output for a given input, you don't need to run the function for the same input twice. They can be easily isolated for testing, since the function is effectively self contained, and the order in which you execute pure functions relative to each other doesn't matter. This makes them great for doing any kind of parallel programming. Without side effects, you can have thousands of threads running the same function, and nothing is going to interfere with each other. Avoiding side effects has become a good practice to avoid programs with multithreading of all sorts. Functional programming languages often to stick to this idea as much as possible. Side effects though are pretty useful for programming though, you generally want to read input from somewhere and output to somewhere. There are some really fancy ways to making the two things mess (e.g. monads).

Back to mathematical functions, what if we had a function that took in the entire state of your computer, and returned a new state. In theory, that is what a computer is actually doing. If it wasn't for concurrency, synchronization, and all the magic that modern microprocessors due do, you could think of evaluating that function as begin analogous to executing a single machine instruction. You computer is just transforming from one state to a next, with each new state just being this function applied to the last. In the theory of computation, that is what we actually do. We think of machines (those being mathematical objects) as transformations (i.e. functions) from one state to the next.

With pseudo-random number generators (pRNGs) don't change any of this. They are just mathematical functions that map the last number they generated plus some amount of extra state, into the next number "random" number with new state. The seed being the initial number and state that it starts from. In practice, many pRNGs get seeded with something relatively predictable, like the exact time your program start running (down to the microsecond). But the computer's clock can be easily wrapped up into the iterative function application to states. The current time just being incremented at each evaluation by a value equal to the time it takes to do it. All of this so far is the deterministic world.

Warning, I'm not physicist or an expert on computer randomness, so take this be as broad overviews at best. For most important stuff (crypto, security, etc.) we want a more truly random value than just a function that jumps all over the place (i.e. pRNG). Exactly what true randomness is a pretty hard thing. Prior to quantum mechanics, many people didn't think it existed (Einstein is famously credited with saying "God does not play dice with the universe"). Computers nowadays get their "real" randomness from a bunch of places, that is entropy, from each and mix them together. There a lot of different sources for entropy inside your computer, an easily understood one is thermal randomness of a CPU. Billions of transistors filling all over the place, in something extremely tiny, make the temperature fluctuate in extremely hard to predict ways. Measure that temp very precisely, and only pull the smallest few digits from it. Is that better or worse than having a webcam pointed at a wall of lava lamps, and using the changes to that image as your entropy source?

Randomness discussions can quickly end up bumping into free will in uncomfortable ways.

So where does all of that leave us? If we ignore true randomness (whatever that may be ), computers are just mathematical functions iterating through states, with the state space being incomprehensively huge. If you include that true randomness, that doesn't seem to work, but very little else (if anything) in reality works either. You have also taken a huge step outside of the realm of CS, and arguably math as well.

1

u/BudgetEye7539 Oct 26 '26

It is also possible to formulate PRNG as a periodic function of some integer argument (index of the sequence member) and some integer parameter. Moreover, the most robust PRNGs are made this way, I mean AES-CTR and ChaCha.

1

u/Yoshibros534 Oct 26 '26

I’m vaguely aware you can simulate crashing in a pure function using something called a “maybe monad”, but i no idea what exactly that means