Command to reset "renpy.random" results?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
FlashFrontal
Newbie
Posts: 10
Joined: Wed Jan 05, 2022 6:14 pm
Contact:

Command to reset "renpy.random" results?

#1 Post 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.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

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

#2 Post 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.
< < insert Rick Cook quote here > >

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

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

#3 Post 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"

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Kocker