Page 1 of 1

Command to reset "renpy.random" results?

Posted: Fri Nov 25, 2022 5:04 am
by FlashFrontal
Hello Ren'Py nerds,

I've noticed that when using something like

Code: Select all

$ Result = renpy.random.randint(1,20)
the Result will be random, but only once per launch of the game. It's the same every time if you roll back, or even when finish the game, returning you to the menu, starting the game again and playing back to the point where Result is determined.

The only ways I've found to actually have it produce different results are to either completely close and open the game (Not even shift-r works it seems), or to reload a save from before Result is determined.

Is there some sort of command I can use to force it to regenerate the seed or whatever it uses to determine what renpy.random.randint generates?

Specifically, a command to force that instance of $ Result = renpy.random.randint(1,20) to produce different results upon rollback? I'm aware I could just put in another instance of $ Result = renpy.random.randint(1,20), but that doesn't solve my specific problem.

Many thanks.

Re: Command to reset "renpy.random" results?

Posted: Fri Nov 25, 2022 5:41 am
by Ocelot
Instances of RenPy random generator are specificaly designed to work together with rollback and provide reproducible determenistic results. The whole point of rollback is to restore state of the whole game including generator states. You could create new generator instances independent from global state, but they will be rolled back the same.

Generators retaining state after exiting to the main menu might be an issue worth reporting on RenPy github (I suppose nobody noticed, because most games are not designed to be played multimple times in one sitting), but rollback is not an issue, it is a feature.

You can create new generator objects $ rnd = renpy.random.Random(seed=None) regularly, where you want unpredicability, seeding it with random number you get externally, not from RenPy (I would disable rolling forward too, since IIRC it expects determenism). Alternatively, you can use Python native rng generators, but in my experience, it still works with rollback, but selectively.

Re: Command to reset "renpy.random" results?

Posted: Fri Nov 25, 2022 12:48 pm
by _ticlock_
FlashFrontal wrote: Fri Nov 25, 2022 5:04 am Is there some sort of command I can use to force it to regenerate the seed or whatever it uses to determine what renpy.random.randint generates?
As Ocelot mentioned, the renpy.random rolls back the random generator state when using rollback (or rollforward). Starting a new game, or loading a previous game will reset the generator.

If you don't want renpy to roll back to previous random generator state, you can simply use random not renpy.random:

Code: Select all

init python:
    import random
    
label start:
    "start"
    $ a = random.random()
    "[a] - NOT rollback friendly random"
    $ b = renpy.random.random()
    "[b] - rollback friendly random"