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/not-just-yeti Oct 26 '26

Lots of good answers in this thread. As a (mostly-functional) programmer here, I'll chime in with a meandering answer:

  • I try to write code as a pure function, when convenient. (It sounds like your colleague is coming from that same mindset.)
  • When writing code that uses an pRNG, I'll write its seed as an input or at least think of it that way. If nothing else, it's a big win for unit-testing.

A common caveat on writing things functionally: If some small helper-function uses an rng, it'd be a massive pain to have the rng be a top-level input which gets passed along to a thousand other functions just because they eventually call the helper which uses the rng.

A strategy here could be: use a top-level global variable; or, have a top-level global hash of "environment variables", or I'd actually use the OS's concept of environment variables. (But those'd be well-documented, and I'm still thinking of them as inputs.) Also, if my language supports optional keyword arguments, then a function's signature makes it clear that there is a dependency on the "global" state, but if the caller doesn't bother passing it in it'll use whatever mock-or-real-objects I defined when starting the program.

  • Similarly for things like timeOfDay() or database connections, I'd have mock generators/versions of these for testing, and think of them as inputs.

When I go to production, then the mock rng/time/database is swapped in with a real one.

I was heartened to recently learn that Rust provides a mock-rng StepRng as part of its standard library! Usually I'm rolling my own for each language (and I'm sure many others are, too). The downside of Rust's particular rng setup is that is StepRng class is that for booleans, it generates false 232 times in a row, before generating true. So I still make my own Rust rng.