Randomness in renpy or Random number generators

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 720
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Randomness in renpy or Random number generators

#1 Post by Andredron »

if you've rollbacked this code and click it through once again then you should get the same result. That's how renpy.random works.

If you need different values after rollback, try to use random.randint(1, 5) instead.
https://www.renpy.org/doc/html/other.html#renpy-random

Code: Select all

# return a random float between 0 and 1
$ randfloat = renpy.random.random()

# return a random integer between 1 and 20
$ d20roll = renpy.random.randint(1, 20)

"Roulette spining"
    python:
        import random
        rlt_drop = random.randint(0, 37)
    "It's a [rlt_drop]"

# return a random element from a list
$ randfruit = renpy.random.choice(['apple', 'orange', 'plum'])

1) Let's look at the simplest example - the game of rock, paper and scissors

Code: Select all

# The game starts here
label start:
    $ rps_beats = [("rock", "scissors"), ("scissors", "paper"), ("paper", "rock")] ### win conditions
    
    "rock scissors and paper"
    
    menu:
        "rock!"
            $ rps_player = "rock" ###what the user's variable will be
        "paper!":
            $ rps_player = "paper." ###what the user's variable will be
        "Scissors!":
            $ rps_player = "scissors." ###what the user's variable will be
            
    $ rps_npc = renpy.random.choice(["rock", "paper", "scissors"]) ###Generate what the enemy will drop off.
    
    e "I chose %(rps_npc)s!"
    
    if (rps_player, rps_npc) in rps_beats: ###win player
        e "You won! Let's play some more."
        jump rps
        
    elif (rps_npc, rps_player) in rps_beats: ###win nps
        e "I won! Let's play again!"
        jump rps
        
    else: ###there is no winner
        e "Tie!  Let's play again."
        jump rps
        
label rps:
    "It's like you knew I'd have [rps_player]!!!" ###player [rps_player]
    "And You kinda knew I'd have [rps_npc]!!!" ###nps [rps_npc]
2) randomization of 2 different variables

Code: Select all

label start:
    $ r1 = 1
    $ r2 = 2
    $ r3 = 0
    
    "What I'm going to do today...."
    
    $ r4 = renpy.random.choice([r1, r2,r3])
    
label selection:
    if r4 == 1:
        "Games".
         jump sleep_home
    if r4 == 2:
        "Play games".
         jump game_psp
    else:
        "Work"
         jump work_a
3) How to display an image randomly
viewtopic.php?p=552289#p552289
viewtopic.php?p=550273
viewtopic.php?p=563533
viewtopic.php?p=511562#p511562

Code: Select all

$ rand = renpy.random.choice(["bg test1","bg test2"])
scene expression rand


#images/bg/test.png
#images/bg/test2.png

else
screen test_screen:
    add "rand"   ###image screen in " "
4) Random number generator in text

Code: Select all

init python:
 
  # Use in format:
  # {txt_f=func}arg{/txt_f}
  # Where:
  # func - The function we need.
  # arg - Arguments of the function.
  # Example:
  # Normal format:
  # renpy.random.choice(('a', 'b', 'c')))
    # in text format:
    # {txt_f=renpy.random.choice}('a', 'b', 'c'){/txt_f}
    config.custom_text_tags["txt_f"] = lambda tag, argument, contents:tuple((kind, unicode(eval("{}({})".format(argument, text)))) for kind, text in contents))
label start:
    "At {txt_f=renpy.random.randint}5, 116{/txt_f} years old, Angelica crossed her grandmother across the street "
    jump start
5) Numerical values

Code: Select all

# The game starts here
label start:
$ money = 7
$ r1 =0
    "I have [money] coins."
    $ r1 = renpy.random.randint(1, 101)
    $ money2 = money + r1
    "When I went home I found [r1] coins."
    "My total is [money2] coins."
Or confuse the user even more

Code: Select all

label start:
    $ money = renpy.random.randint(1, 101)
    $ r1 = renpy.random.randint(1, 101)
    $ money2 = money + r1
    "I have [money] coins."
    "When I went home I found [r1] coins."
    "My total of coins is [money2]."
6) How to prescribe jumps randomized by tags

Code: Select all

$ choice = renpy.random.choice(("triv18", "triv28", "triv38", "triv48"))
jump expression choice

# Or even:
jump expression renpy.random.choice(("triv18", "triv28", "triv38", "triv48"))

Code: Select all

label loc:
 #zero value at the beginning of the action.
    $ gtm = 0
    "Here is the character's cue. "
 
#Generate a random integer between 1 and 100.
    $ gtm = renpy.random.randint(1, 100)
    
    ###with the highest value put on the 1st place and by decreasing value
    
    if gtm >= 90: #90 and up
       jump loc1
       
    elif gtm >= 70:  #up 70 to 89
       jump loc2
       
    else: #to 70
       jump loc3

Code: Select all

init python:
	def getRandomLabel():
		labels = (
			"l1", "l2"
		) # your labels...
		
		return renpy.random.choice(labels)
			
label start:
	"get ready... the next click will take you randomly to some label..."
	jump expression getRandomLabel() # use `jump expression` to jump to make the jump dynamic according to the expression given...

# other.rpy
label  l1:
	"label 1"
	
label l2:
	"label 2"
7) Generate a number from 0 to 1 (example 0.35)

Code: Select all

screen main_menu:
    # This replaces the other menus.
    tag menu
    # The background of the main menu.
    window:
        style "mm_root"
    # Main menu buttons.
    frame:
        style_group "mm"
        #############################
        xalign renpy.random.random()
        yalign renpy.random.random()
        #############################
        has vbox
        textbutton _("Start Game") action Start()
        textbutton _("Load Game") action ShowMenu("load")
        textbutton _("Settings") action ShowMenu("preferences")
        textbutton _("Help") action Help()
        textbutton _("Quit") action Quit(confirm=False)
8) Randomized order
random.shuffle

Code: Select all

$ playlist = ["song1.mp3", "song2.mp3", "song3.mp3", "song4.mp3", "song5.mp3"] 

$ renpy.random.shuffle(playlist)

play music playlist fadeout 1.0 fadein 1.0

Code: Select all

 
$ items = [1, 2, 3, 4, 5, 6, 7] # at the beginning

$ random.shuffle(items) # command

$ items = [7, 3, 2, 5, 6, 4, 1] # we can get
 
9) take a few values at random

Code: Select all

label start:
    $ money = 7
    $ r1 =0
    "I have [money] coins"
    $ r1 = renpy.random.sample([1, 2, 3, 4, 5], 3)
"When I went home I found 3 coins of [r1]rubles."
10) A short way to make a random jump to a label

Code: Select all

$ first_encounter = renpy.random.choice(['rencontre_fairi', 'rencontre_nook', 'rencontre_paul'])

jump expression first_encounter
11) Generator random name
viewtopic.php?p=566574#p566574

12) Imitation Baldur's Gate dice animation effect
viewtopic.php?p=566269#p566269

13) d20-friendly Visual Novel prototype
https://github.com/sprintingkiwi/dungeon_academy

14) blink animation modif
viewtopic.php?p=561888#p561888

15) Picking random idle poses
viewtopic.php?p=553693

16) print out random numbers in random text(Additional Questions)
viewtopic.php?p=560021

17) Screen with random words
viewtopic.php?p=553494#p553494

18) Limited number of questions
viewtopic.php?p=548798#p548798

19) Random-bag
https://patreon.renpy.org/python-tricks ... random-bag

20) Dixylsea(text shuffling' effect)
viewtopic.php?p=533698

21) Timed Choice Menus with timeout random choice
viewtopic.php?p=533452#p533452

22) Three Creator Defined Statements
https://patreon.renpy.org/three-creator ... tml#random

Code: Select all

random:
    e "This occurs one quarter of the time."
    e "This also happens one quarter of the time."
    weight 2 e "This happens two quarters of the time - or half the time!"


e "Let's check the weather."

random:
    scene bg hurricane
    scene bg hailstorm
    scene bg frograin

e "Yeah, looks like a good day to stay inside and add statements to Ren'Py."

23) randomizing transitions in game
viewtopic.php?p=528827

24) randomize hover_sound
viewtopic.php?p=527782

25) random sound
viewtopic.php?p=523730

26) Random Rewards on Click
viewtopic.php?p=523409

27) Random Pause Duration
viewtopic.php?p=523212

28) Voting for someone based on a percentage
viewtopic.php?p=521861

29) Random NPC generator
viewtopic.php?p=520166

30) Capping a random variable
viewtopic.php?p=519547

31) Setting possibilities/chance
viewtopic.php?p=518399

32) picking the 3 highest from the list and than set to random choice
viewtopic.php?p=517993

33) How to make the first line of a menu dynamic
viewtopic.php?p=514639

34) Random Music Generator
viewtopic.php?p=514019

35) Reading randomized dialogue from file
viewtopic.php?t=31698&start=15

36) This code selects two random elements from a list

Code: Select all

mylist = ["Eileen", "Lucy", "Lemma", "Monika"]
friends = renpy.random.sample(mylist, 2)

Post Reply

Who is online

Users browsing this forum: Google [Bot]