How to get random value in renpy

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
User avatar
rururubell
Newbie
Posts: 13
Joined: Mon Sep 30, 2019 4:00 am
Deviantart: rururubell
Contact:

How to get random value in renpy

#1 Post by rururubell »

I make a game, that has a lot of animated background and other arts. Because of that, I need a lot of random values. But I read some posts on this forum, and on another places, and it looks like that on renpy I can't use python random functions. It get me random value one time and on second time it get me same value. And only way to get a random value, is use renpy.random.choice( ... )

Is that right? Or maybe here is a way, and not only choice() can return random, int value? I read some posts, but because my English not so good, I think, maybe I miss something?
Sorry for my English!

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

Re: How to get random value in renpy

#2 Post by _ticlock_ »

Hi, rururubell,

Actually renpy.random.random(), renpy.random.randint(a,b) gives random numbers, but it also returns to the previous state with rollback and rollforward.

User avatar
rururubell
Newbie
Posts: 13
Joined: Mon Sep 30, 2019 4:00 am
Deviantart: rururubell
Contact:

Re: How to get random value in renpy

#3 Post by rururubell »

Hi, _ticlock_

I don't think it's problem if random() get same value after rollback and forward. But this atl code, for example doesn't work well.

Code: Select all

    contains:
        "some_image"
        xpos renpy.random.randint( 0, 1920 )
        ypos renpy.random.randint( 0, 1080 )
        pause 1
        repeat
If I use this code, "some_image" get some random position on screen but doesn't move. If I use this code:

Code: Select all

    contains:
        "some_image"
        xpos renpy.random.randint( 0, 1920 )
        ypos renpy.random.randint( 0, 1080 )
        pause 1
        xpos renpy.random.randint( 0, 1920 )
        ypos renpy.random.randint( 0, 1080 )
        pause 1
        repeat
"some_image" can move between random position, but it was randomized once. After all "some_image" will always move between two same positions. And I can get random positions, only if I use .choice()
Sorry for my English!

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

Re: How to get random value in renpy

#4 Post by _ticlock_ »

I think transform might not be very convenient for what you are trying to do. Consider DynamicDisplayable or CDD.
Anyway, for transform you can do something like this:

Code: Select all

init -1 python:
    def t_func(trans,st,at):
        if st > 1:
            trans.xpos = renpy.random.randint( 0, 1280 )
            trans.ypos = renpy.random.randint( 0, 720 )
            return None
        else:
            return 0.05

image rand_pos:
    "test.png"
    block:
        function t_func
        pause 1.0
        repeat

label start:
    show rand_pos
    "test.png changes its pos every 1 second"

Post Reply

Who is online

Users browsing this forum: No registered users