renpy.random Question

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
DigiBloo
Newbie
Posts: 2
Joined: Fri Jan 20, 2006 8:02 pm
Contact:

renpy.random Question

#1 Post by DigiBloo »

If I have two renpy.random.randoms in my script, will they generate separate numbers? Or would they still use the previous number generated? I know that it participates with rollback, but I'm not sure what happens.

What exactly does renpy.random.random do? Does it actually generate a number between 0 and 1?

Example:
Let's say Bill throws a die. He has to get it over 4, or he'll lose. The chance of him getting it over 4 is 20%.
In another somewhat unrelevant event, Mary spins a bottle. The chances of it landing on the boy she likes is 15%.

So if they generate the same number, then that would mean if Bill lost, Mary wouldn't have the bottle land on the boy she likes.




I want them to generate completely different numbers. Is that possible?

And if they generate the same number, is there any way that I can get them to generate two separate numbers? Like, can I label them or something?

Thanks in advance!! ^^

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#2 Post by PyTom »

The best way to think about renpy.random.random is to think that each time Ren'Py starts, an infinite list of random numbers is created, and a pointer starts off at the start of this list. Everytime you take a random number, this pointer gets advanced one spot down the list, and the number at the previous spot is returned.

The pointer is part of the state that participates in rollback, so when the user goes back it time, the pointer moves back with him. So if the user takes the same path through the game, the same random numbers will be generated at the same points.

If the user rolls back and then takes a different path, the same numbers will be generated at different points.

If the user never rolls back, this is completely academic. Without rollback, every call to renpy.random.random() will generate a new random number.

Basically, renpy.random.random does the right thing in almost all cases.

Please note that use of random numbers in a b-game is discouraged, as it makes it harder to clear all paths in a story, and doesn't usually provide enough gameplay to make actual gamers interested.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
DaFool
Lemma-Class Veteran
Posts: 4171
Joined: Tue Aug 01, 2006 12:39 pm
Contact:

#3 Post by DaFool »

I'm bumping this thread and also including an example found in the forum search.

Code: Select all

label rps: 

    menu: 
        "Rock!": 
            $ rps_player = "rock" 
        "Paper!": 
            $ rps_player = "paper" 
        "Scissors!": 
            $ rps_player = "scissors" 
            
    $ rps_npc = renpy.random.choice(["rock", "paper", "scissors"]) 

    e "I choose %(rps_npc)s!" 

    if (rps_player, rps_npc) in rps_beats: 

        e "You beat me!" 

    elif (rps_npc, rps_player) in rps_beats: 

        e "I win!" 

    else: 

        e "It's a tie! Let's do it again!" 
        jump rps 

    # ... 
Ahhh...more random generator-related posts:

http://lemmasoft.renai.us/forums/viewto ... dom+number
http://lemmasoft.renai.us/forums/viewto ... dom+number

Post Reply

Who is online

Users browsing this forum: No registered users