Show success rate with non-uniform probability distribution.

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
Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

Show success rate with non-uniform probability distribution.

#1 Post by Uy27 »

Hello everyone
So I have this code;

Code: Select all

    $ results = [('success',1 +atk),('fail',10)]
    $ place = NonUniformRandom(results)
Works fine and as it supposed to. Question is;
Is there a way to show the success chance in a text?

Like that code above and let's say that atk is 1 so the success probability is 2.
Can it be in a text like "The success rate is "here"?

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Show success rate with non-uniform probability distribution.

#2 Post by Per K Grok »


Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

Re: Show success rate with non-uniform probability distribution.

#3 Post by Uy27 »

Per K Grok wrote: Tue Jul 31, 2018 11:17 am Check this in the manual

https://www.renpy.org/doc/html/text.html
Hello, thanks for taking your time to reply to my question.
I have of course seen that and I also know how to add variables in the text.
It's more than I don't know what in the code is the variable that will show that success rate.

Also, I assume another step which is needed for it to calculate the success and put it into a code that will show it. And that is where I need help with.

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Show success rate with non-uniform probability distribution.

#4 Post by MaydohMaydoh »

When you want to access something in a list or tuple, you can use indexing. In this case you want to access the first tuple in the list which would be
results[0]
Which would return ('success',1 +atk). But you want to access the second value in the tuple so you use
results[0][1]
Which returns the success rate, whatever value 1 + atk is.
Is this what you are looking for?

Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

Re: Show success rate with non-uniform probability distribution.

#5 Post by Uy27 »

Not quite.
I think it may be my explanation that is lacking so I apologize for that.

Code: Select all

    $ results = [('success',1),('fail',9)]
    $ place = NonUniformRandom(results)
I changed it to 1 and 9 to explain it better. In the renpy documents, this means on average out of 10 times. 1 will be success and 9 will be fails. So a 10% chance for success.
I want to know if there is a way that can calculate that % and show it in a text. Or if there even is a way to do it with that code.

I hope this explains it better.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Show success rate with non-uniform probability distribution.

#6 Post by Remix »

$ prob = "{:.1%}".format( float(results[0][1]) / float(results[0][1] + results[1][1]) )
"[prob]" >> "10.0%"

*should* work (might need [prob!q] though as the % should be added by pyformat
Frameworks & Scriptlets:

Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

Re: Show success rate with non-uniform probability distribution.

#7 Post by Uy27 »

Thank you so much for that Remix! It works.
However, is there a way for it to appear before you 'run the code'".

Example of the code;

Code: Select all

label theft1:

    while dayTime ==  4:
        scene black
        "It's late. I better get some sleep."
        return

    $ results = [('success',1 +pc.stealth),('fail',3)]
    $ place = NonUniformRandom(results)
    $ prob = "{:.1%}".format( float(results[0][1]) / float(results[0][1] + results[1][1]) )
    $ renpy.transition(longfade)

    if place.pick() == 'success' or pc.stealth >= 20:
        scene success with longfademore
        "Success!"
        $ missionS += 1
        $ dayTime += 1
        $ expEarned = renpy.random.randint (5, 15)
        if pc.exp >= 750:
            "You reached max experience points for this version of the game."
        else:
            $ pc.exp += expEarned
            "You got [expEarned] exp."
        $ moneyEarned = renpy.random.randint(15, 25)
        $ pc.money += moneyEarned
        "You earned $[moneyEarned]."
        call expression 'levelUp' pass ('end') from _call_expression
        return
This gets called from a screen. The % shows 0 at first (How I set it as default) and once you call the theft1 label it then shows the percent of the success rate.
However, is there a way I can see the percent before running the program? Especially since the " +pc.stealth" part change once you equip an item. So being able to see the success difference without running though it would be great.
I have other labels of this same code but I assume I can just change the prob to prob2 etc.

Also sorry MaydohMaydoh , seems like you were actually right. I just couldn't figuere out to use those index things.

Post Reply

Who is online

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