Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Distributing probability with a set of elements

Greetings! This is my first time stopping by the forums to ask a question. So far all the docs and discussions have been fabulous and allowed me to get deep into Twine. But now it seems I'm trying to do something unusual and I could use some help. Thanks in advance for taking the time to look.


I am creating a system for random weather in a small game I'm working on. At any given time based on certain factors, there are varying chances of different types of weather.

For example, let's say we have three variables. Something like this:

$weatherChance.cloudy, $weatherChance.rainy, and $weatherChance.sunny.

For the purposes of this discussion there's a 0.2 chance of clouds, a 0.3 chance of rain, and a 0.5 chance of sun. Together they add up to 1, or a 100% chance of getting one of these three variables with a random number between 0 and 1. Let's map them visually:

| cloud |  rain  |      sun      |
0                                  1

Now let's get a random number between 0 and 1. Maybe that number is 0.7. By itself it is greater than any of the variables named above, but within the distribution I've described it should land within the range of sun and give us a sunny day.

My question is this: how do I bend Twine into letting me set up this kind of system? I have the set of weather probabilities, all adding up to 100% as I have named above. I have a random number generator rolling the dice for something inside that range. I just don't know how to set up the variables in such a way to tell Twine to evaluate a set of numbers and yield the correct answer. I've dug into the SugarCube docs and I think there are functions for this, but I don't know how to implement the system.


I asked a programmer friend this question and his answer was to use Javascript. That's fine with me, but I feel like it should be possible with some variable trickery. The math is a little over my head so at first I wasn't even sure how to ask the question.

References:
http://www.motoslave.net/sugarcube/docs/#functions-randomfloat
http://en.wikipedia.org/wiki/Probability_distribution#Discrete_probability_distribution

Comments

  • Assuming the conditions of your map then the following should do what you want:
    (To Test the code place it in your Start passage and use refresh (F5) to generate a new random number.)

    <<set $weatherChance = {
    cloudy: 0.2,
    rainy: 0.3,
    sunny: 0.5
    }>>

    Cloudy: <<print $weatherChance.cloudy>>
    Rainy: <<print $weatherChance.rainy>>
    Sunny: <<print $weatherChance.sunny>>

    <<set $random to randomFloat(1.0)>>
    Random: <<print $random>>

    <<if $random <= $weatherChance.cloudy>>
    <<set $weather to "Cloudy">>
    <<elseif $random <= ($weatherChance.cloudy + $weatherChance.rainy)>>
    <<set $weather to "Rainy">>
    <<else>>
    <<set $weather to "Sunny">>
    <</if>>
    Weather: <<print $weather>>
    It works because the chance of Clouds (0.2) on your map is between 0.0 and 0.2, the chance of Rain (0.3) is between 0.2x and 0.5, and the chance of Sunny (0.5) is between 0.5x and 1.0.
  • Hmm, you're right greyelf. It really is that easy, I was just focused on the wrong things. Thank you for you assistance.
Sign In or Register to comment.