HTML5 JS Updated PRNG (pseudo-random number generator)

adent42adent42 Key Master, Head Chef, Executive Chef, Member, PRO Posts: 3,004

Pre-release note for people using the HTML5 engine, for people to prepare for.

TL;DR: A future HTML5 engine release will respect the randomSeed attribute. If you don't care about that, set randomSeed to -1 and it will behave like before.


Some background:

Random functions on computers are not "truly" random and instead are pseudo-random number generators (prng's). A math function returns a new number every time that's close enough to random to be usable. AFAIK, all random number algorithms need what is called a "seed" to kick off the random number sequence.

In the native engine, you can adjust the seed by setting the game level randomSeed attribute. If you didn't care about the seed and wanted different randomness every time, you would just set the randomSeed attribute to something related to device.time or set it to -1 which seeds with default "random" value (which usually includes stuff like the current time, data from some random memory location, etc).

In javascript, we ignored this because javascript's random number API is not seed-able. We hesitated in the past because using a javascript based PRNG is always going to be slower than the one built into Javascript.

As we started building out our blockchain games, we realized that we needed to make it possible to replay a game session that uses random numbers to check for cheating.

So future versions of the GameSalad HTML5 engine will work as follows:

If you set randomSeed to -1, you will be using Javascript's unseedable but fast Math.random function for all random values.

If you set anything else, then you will be using the javascript based PRNG that will generate the same sequence of random numbers for a given seed.

We'll likely deploy it sometime this week, and we'll let everyone know here when we do. But for now, I just wanted to prepare any HTML5 developers of what's about to happen, so they can adjust their games accordingly.


  • adam36021adam36021 Member, PRO Posts: 45
    edited April 2023

    This is interesting. So if we set a seed value, the javascript PRNG would be pre-determined?

    So for example, if i am creating a game with multiplayer... 2 clients running a simultaneous simulation. If both clients have the same seed, and execute RNG at the same time, they will have exact results?

  • adent42adent42 Key Master, Head Chef, Executive Chef, Member, PRO Posts: 3,004

    @adam36021 that's the idea! We haven't pushed this yet (found an unrelated bug), but it should be in web creator by this weekend and will be deployed as part of the main packages shortly after.

    Since GameSalad doesn't have great RT Multiplayer capabilities, where I tend to see this is either for anti-cheat detection (verify some really played a level to claim a prize) or for ghost based competitive multiplayer (users compete by playing the same game with the same seed).

    The main kink in multiplayer will be game engine timing. Our engine isn't built around guaranteed performance on all systems, so if the random numbers are called at different times, then a user might get different experiences.

    You'd likely have a server that creates a game "session" that also defines a common seed. Then anyone playing in the session / tournament / game would get the same RNG.

  • adam36021adam36021 Member, PRO Posts: 45

    cool. sounds good, i am interested in toying with this.

    and yeah, i think i will face serious desync issues trying to do anything remotely real time as the execution of logic seems to vary slightly from device to device. so the simulation stuff is maybe not a fit for GS

    But there's an enormous amount you could do with random seed -- like competitive single player strategy with leaderboards, daily challenges that are the same for all players, etc.

Sign In or Register to comment.