Random select an event to jump, percentage likelihood?

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
User avatar
Kate
Regular
Posts: 197
Joined: Thu Sep 19, 2013 6:10 pm
Projects: Blackout
Organization: Moonlight Otome
Location: United States
Contact:

Random select an event to jump, percentage likelihood?

#1 Post by Kate »

This is what I have in mind for my game, and I'd like to know if it is possible:

There will be several short routes within the game that can be selected at random. Basically, one is selected after the game starts (the first thing that happens) and then, once the route is played out, the player can re-start the game and have a chance at playing another route or the same route. I'm looking to program 7 short routes, 4 being more likely (60% or so), 2 being less likely (30%) and a rare route (10% chance) for replay value for the game and alternate options. Kind of like a short, Clue-style Whodunnit. (Like a five-minute mystery)

How do I create a list with probability that has these events (the routes), and how do I include the jumps to the labels (either within the list or the if, elif, else statements?) ?

I'm having trouble finding an example that suits my needs for this, and I'm new to programming. Help please!
Current Project:
Blackout [VN][Romance][GxB][Mystery][Suspense] http://lemmasoft.renai.us/forums/viewto ... 43&t=34118
"It is the duty of authors to make the fantastic seem ordinary and the ordinary seem fantastic." - K. Auer

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Random select an event to jump, percentage likelihood?

#2 Post by Asceai »

Code: Select all

init python:
    def WeightedChoice(choices):
        """
        @param choices: A list of (choice, weight) tuples. Returns a random
        choice (using renpy.random as the random number generator)
        """
        totalweight = 0.0
        for choice, weight in choices:
            totalweight += weight
        randval = renpy.random.random() * totalweight
        for choice, weight in choices:
            if randval <= weight:
                return choice
            else:
                randval -= weight

label start:
    $rand_choice = WeightedChoice([("Route_A", 0.50),
                                   ("Route_B", 0.35),
                                   ("Route_C", 0.10),
                                   ("Route_D", 0.05)])
    jump expression rand_choice
    return
  
label Route_A:
    "This is Route A"
    return
    
label Route_B:
    "This is Route B"
    return
    
label Route_C:
    "This is Route C"
    return
    
label Route_D:
    "This is Route D"
    return
    
The first requirement, that of a weighted equivalent to renpy.random.choice(), is fulfilled by the implementation of the WeightedChoice() function given in the code. The second requirement, to be able to jump to the correct label, is fulfilled by the 'jump expression' statement, which, like 'show expression' etc. takes a Python simple expression instead of the direct name of the label.
Note that it is not necessary for the weights to add up to 1 or anything else - but if they add up to 1, it is easier to visibly associate them with a given percentage chance.

User avatar
Kate
Regular
Posts: 197
Joined: Thu Sep 19, 2013 6:10 pm
Projects: Blackout
Organization: Moonlight Otome
Location: United States
Contact:

Re: Random select an event to jump, percentage likelihood?

#3 Post by Kate »

Thank you so much! It works perfectly. I was racking my brain trying to come up with something and I didn't know it needed a python init block.. :)
Current Project:
Blackout [VN][Romance][GxB][Mystery][Suspense] http://lemmasoft.renai.us/forums/viewto ... 43&t=34118
"It is the duty of authors to make the fantastic seem ordinary and the ordinary seem fantastic." - K. Auer

Dharker
Regular
Posts: 99
Joined: Sun Sep 15, 2013 6:45 am
Contact:

Re: Random select an event to jump, percentage likelihood?

#4 Post by Dharker »

Thank you, that actually does even more than I wanted, the weighted choice gives me some extra possibilities. Thanks for that.

User avatar
burnt_offering
Regular
Posts: 107
Joined: Sun Feb 23, 2014 4:21 pm
Completed: The Stroke of Midnight
Organization: Ace Of Spades
IRC Nick: burnt_offering or TheTapDancer
Skype: aceofspadesvn
itch: aceofspadesvn
Location: London
Contact:

Re: Random select an event to jump, percentage likelihood?

#5 Post by burnt_offering »

I had a similar idea, but I used a pseudorandom generator where the seed was generated from the time, so you could only actually get specific routes at any time but to the uninformed it would appear random.
Wouldn't advise it, took some serious coding.
[url]http:\\aceofspades@vnovel.com[/url]
Or just follow me at @AceOfSpades_VN

Post Reply

Who is online

Users browsing this forum: No registered users