How I deal with the *rand stats screen bug

So the *rand stats screen bug is a classic at this point but for those unaware, this bug occurs when you use *rand on a page, enter the Stats screen and return. This forces the page you were on to reload and your previous *rand result will be overridden. This allows readers to re-roll random outcomes.

The general solution is to generate the random result on a previous page. This means that even if the Stats screen is entered, it won’t override / re-roll the random result.

My project uses *rand quite liberally and it was a pain to remember to add in *rand to the previous page, so I decided to pre-roll all possible outcomes as early as possible. It’s not a perfect solution but the implementation is easy to use and less error-prone. I thought someone else might also find it useful, so decided to share it here.

To use it, add the following file to your project.

subroutine_random.txt
====================================================
*label rand
*params min max
*temp random random_array[random_current_index]
*gosub normalise_random random min max
*comment random_array[${random_current_index}]=${random} -> ${random_result}
*comment *line_break
*set random_current_index +1
*return

*label populate_random_array
*temp max_loops random_array_count
*temp i 1
*label loop
*if (i <= max_loops)
	*gosub generate_random
	*set random_array[i] random_result
	*set i +1
	*goto loop
*return

*label generate_random
*temp r ""
*rand r 0 (random_precision - 1)
*set random_result r / random_precision
*return

*label normalise_random
*params random min max
*temp range (max - min) + 1
*temp raw_scaled random * range
*temp offset_scaled raw_scaled + min
*gosub floor offset_scaled
*return

*label floor
*params number
*temp floatingPoint (number modulo 1)
*temp integerPart (number - floatingPoint)
*set random_result integerPart
*return

Then add the following code to the top of your startup.txt:

*create_array random_array 1000 ""
*create random_current_index 1
*create random_precision 1000000
*create random_result ""
*gosub_scene subroutine_random populate_random_array

Now whenever you want a random number, you just call the subroutine:

*gosub_scene subroutine_random rand 1 100
You rolled a ${random_result}!

The benefit of this is that you don’t need to remember to add anything to the previous page. You call it exactly when you need it.

Some things to note:

  • By default, the array stores 1000 unique rolls. You can increase this if needed but be mindful of performance / memory issues. You only need to increase this if you’re planning to have >1000 random rolls over the course of a playthrough. Note: you will also need to update the loop limit. If you’re still having performance issues, you may want to have multiple smaller arrays (per scene) instead of one big global array.
  • By default, the randomness precision is set to 1000000. You can easily increase this if needed but be mindful of performance / memory issues. You only need to increase this if your random range will be larger than 1000000 and you need each number in the range to be distinct (this is extremely unlikely).
  • Any subroutines that return data will store it in a global variable called random_result. If you’re familiar with other ChoiceScript libraries, it’s similar to how they usually do it too. The variable name can be changed fairly easily if you need to.
  • You can remove the comments from the subroutine rand method to view more details about the random roll that was generated.

You can view the live example (and code) here: CoGDemos

Anyways, I’m curious how other people deal with this issue so please do share your own solutions! I’m sure there’s more elegant waysout there but this does the job for me.

5 Likes

I made a pseudo-random number generator a while back but honestly I’m not sure many people mind that *rand changes when you visit the stats, or use it enough for it to be an issue.

Rolling it all beforehand is pretty novel!

2 Likes

I think most people roll it on the previous page to avoid rerolling. I’d rather consider it a feature rather than bug and allow it, but that’s just me.

5 Likes

There are occasions where it can cause a big issue though. I’ve used rand to set event outcomes, riddles and scene selections, so having it jump wildly around is really disorientating, and in other cases can cause extra timer stats and the like to be readded so it breaks the game because it doesn’t negate the previous result’s change in stats if they were changed on the first roll. It’s normally not a big deal, but when i working on dragon chronicles which had lots of rerolls within scenes, it was an absolute pain to sort out in places so it was stable.

6 Likes

Yeah it’s a huge pain in the ass. I usually just do it this way:

List at top of startup:

*rand variable_1 1 100
*rand variable_2 0 1
*rand etc etc etc etc etc

Every time I need a random variable:

${variable_1} (call the variable)
*rand variable_1 1 100 (re-randomise the variable for next time)

As long as you *rand after the call, you’re good.

5 Likes

Obviously, and you of course need to take the rerollability into account in your design if you want to use it; but having something be a bug in a game doesn’t mean it’s a bug in the engine which, from what I understand, is acting as intended.

Ahh that’s really neat! That’s a lot cleaner than my solution and avoids the messiness of having large arrays.

I hadn’t actually considered reloading save games! For your issue in Breach, I think that can be both good and bad. I can easily see a player trying to reload to see what happens in a different scene (instead of needing to replay the whole game). I suppose that’s a game design decision.

Could they fix the bug?

It’s a design choice and not a bug, from what I can see.

1 Like

Huh… interesting!