Randomizing

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
Wright1000
Miko-Class Veteran
Posts: 629
Joined: Thu Mar 31, 2011 10:20 am
Completed: Finding A Murderer, Memory Loss, Crime Investigation, Stay away from the graveyard, Last Day at School, Lonesome, Email, Hired Gun, Dusk, Hired Gun 2, Man-at-arms, Hired Gun 3, The Phantom Caller, Street Girl, Free love, The Story of Isabel Claudia
Contact:

Randomizing

#1 Post by Wright1000 » Mon Apr 25, 2011 4:55 am

If I want to randomize something, how do I do it?

For example, I want a non-player character to choose a number between 1 and 10, how is it done?

For example, two non-player characters are playing football. I want to randomize the winner. How is it done?
He who doesn't care about the environment doesn't care about his grandchildren.


User avatar
Tachyglossus
Regular
Posts: 164
Joined: Wed Dec 01, 2010 7:43 pm
Projects: Sprite Art for: "Final Banquet", "365 Days"
Location: British Columbia
Contact:

Re: Randomizing

#3 Post by Tachyglossus » Mon Apr 25, 2011 1:33 pm

Ooooh speaking of randoms, this is a purely theoretical question, but is it possible to make it pull a random element from a list, but attach a greater probability to certain items for being chosen?

Using the fruit example in the reference, say you wanted oranges to be twice as more likely to be picked than apples or plums, would that be possible?

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Randomizing

#4 Post by Alex » Mon Apr 25, 2011 3:06 pm

Mmm..., the easiest way is to add twice more oranges in a list, so the probability of choosing an orange from that list would be twice greater.
More complicated way described in a Cookbook.

pondrthis
Veteran
Posts: 265
Joined: Tue Dec 14, 2010 5:20 pm
Completed: G-Senjou no Maou Translation
Location: Nashville, TN
Contact:

Re: Randomizing

#5 Post by pondrthis » Mon Apr 25, 2011 3:38 pm

Out of further curiosity, is there no built-in generator for the normal distribution? (I'm obviously changing the topic from discrete to continuous space with this question, but it's only a matter of rounding.)

User avatar
PyTom
Ren'Py Creator
Posts: 15893
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:

Re: Randomizing

#6 Post by PyTom » Mon Apr 25, 2011 4:08 pm

Ren'Py gives you all of the functions in the Python random module. It looks like the gauss function is the one you want - so renpy.random.gauss.

http://docs.python.org/library/random.html#random.gauss
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
HotLimit
Regular
Posts: 48
Joined: Thu Jun 18, 2009 4:04 pm
Contact:

Re: Randomizing

#7 Post by HotLimit » Mon Apr 25, 2011 6:56 pm

Tachyglossus wrote:Ooooh speaking of randoms, this is a purely theoretical question, but is it possible to make it pull a random element from a list, but attach a greater probability to certain items for being chosen?

Using the fruit example in the reference, say you wanted oranges to be twice as more likely to be picked than apples or plums, would that be possible?
I just write a series of "if [variable]" statements, with the range of available answers for [variable] corresponding to each of the possible items. I'm a little rusty with Ren'Py, but I believe it would go something like this:

Code: Select all

$ x = renpy.random.randint(1, 100)

if x > 0 and x < 26:
    n "You got an apple."

elif x > 25 and x < 51:
    n "You got a plum."

elif x > 50:
    n "You got an orange."
You can use any range you want for your variable, I just chose 1-100 because percentages are easy to remember in 100s (what's 50% of 100? 50!)

Plus you can always build on each possibility to make things even more diverse:

Code: Select all

$ x = renpy.random.randint(1, 100)
$ y = renpy.random.randint(1, 3)

if x > 0 and x < 26:
    if y == 1:
        n "You got an apple."

    elif y == 2:
        n "You got an orange."

    elif y == 3:
        n "You got a plum."

else:
    n "You were bit by a snake and died."


User avatar
Tachyglossus
Regular
Posts: 164
Joined: Wed Dec 01, 2010 7:43 pm
Projects: Sprite Art for: "Final Banquet", "365 Days"
Location: British Columbia
Contact:

Re: Randomizing

#8 Post by Tachyglossus » Mon Apr 25, 2011 8:56 pm

Thank you these are all awesome answers I'll save for later use!
Last edited by Tachyglossus on Thu May 19, 2011 12:49 pm, edited 1 time in total.

User avatar
Wright1000
Miko-Class Veteran
Posts: 629
Joined: Thu Mar 31, 2011 10:20 am
Completed: Finding A Murderer, Memory Loss, Crime Investigation, Stay away from the graveyard, Last Day at School, Lonesome, Email, Hired Gun, Dusk, Hired Gun 2, Man-at-arms, Hired Gun 3, The Phantom Caller, Street Girl, Free love, The Story of Isabel Claudia
Contact:

Re: Randomizing

#9 Post by Wright1000 » Tue Apr 26, 2011 9:12 am

Thank you, everyone.
Your help was useful.
He who doesn't care about the environment doesn't care about his grandchildren.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], _ticlock_