Has anyone ever precalculated an array of random numbers?

Wondering about people’s experience simulating random numbers.

Integrating user interactions, I imagine you could loop through a limited list of precalculated random values with some shared periodic seed (greenwhich time?) to make for nice deterministic (multiplayer) stochastic outcomes?

Maybe I’m overthinking it. Wondering your thoughts.

The fewer calls to random the better ?

Like there is a blockchain discussion
Maybe they already had it though

Hi withADoveInOneHand,

Short answer: yes, pre-calculating random numbers is a fairly common practice, especially in the context of random textures for use in procedural generation.

Longer answer: while it’s not uncommon to preemptively calculate random numbers and cache them, doing so may not be as necessary or advantageous as you might think.

  • With regards to being able to reproduce results across different locations, note that random number generators are actually pseudorandom number generators. As long as you’re allowed to specify the seed (without which it will probably auto-select some effectively random seed, like system clock), a generator’s behavior already is deterministic. In other words, a pseudorandom number generator (which I will now call RNG for short) will always generate the same sequence of numbers given the same seed, which is effectively the same as if it were iterating through an array of cached values.
  • Given the above, the primary behavioral difference between a cached solution and just running the same RNG twice with the same seed is period: the cached solution will “loop” after a certain number of values whereas most standard RNGs won’t. (NOTE: I think they actually might, but the looping period is so long that it would never come up.) In most cases, looping is undesirable; but if it’s actually helpful for your use case, there are older RNG models that can naturally provide looping behavior with a reasonably short period.
  • Whether or not caching is right for you will almost entirely depend on your use case. The question of whether it’s more expensive to pre-compute or to cache is just the classic trade-off between cycles and storage, but in most cases both costs will be extremely cheap. Ultimately, I think the question will probably come down to (1) what quality of “random” you need, (2) how frequently you anticipate querying new random numbers, and (3) what’s your preferred strategy to synchronize RNGs across multiple entities, when relevant.

Don’t know if I answered any of your questions there, but hopefully at least some of that was helpful. :smiley:

Yes great clarification thank you. Using .js it looks like I will need some sort of aftermarket RNG that is seedable, cached or not.

And to keep sync among players I just make sure it is called in the same sequence…