Howdy, Stranger!

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

Random Rolls?

I've been playing around with Twine a lot and I still learn new things and there are all sorts of things for me to do in Twine. So I figured it was time for me to get into the big-boy scripting.

For instance, random rolls. Like a D-20 or so. And if a certain number is rolled, or randomly generated, I could use that to my advantage by playing out different scenarios in my game. Like for example, I open a passage and a random number is rolled as the player-character finds loot, like coins or something. A random amount of coins.

Is there a script or a macro for random rolls? Like, I dunno, something simple like <<roll 20 $money>> for a random number between 1 and 20 to add to $money.

Comments

  • either(1,2,3,4,5,6)
    will roll a d6.
  • mykael wrote:
    either(1,2,3,4,5,6)
    will roll a d6.


    Okay... Now how do I use it to bring up different passages? How can I add it to a $money variable?
  • <<set $money = Math.floor(Math.random()*6)>>
  • Jinx wrote:

    Is there a script or a macro for random rolls? Like, I dunno, something simple like <<roll 20 $money>> for a random number between 1 and 20 to add to $money.


    I believe you're using SugarCube, aren't you?

    If so, you can use the random(min, max) utility function.  For example:

    // returns a whole number within the range: 16
    random(1, 6)

    // returns a whole number within the range: 120
    random(1, 20)

    // simulate a 3d6 roll
    random(1, 6) + random(1, 6) + random(1, 6)
    To use that for an amount of coins, let's say, you might do something like these:

    // sets $money to randomly selected amount in the range: 120
    <<set $money to random(1, 20)>>

    // sets $money to randomly selected amount in the range: 1050
    <<set $money to random(10, 50)>>

    Wraithbane wrote:
    <<set $money = Math.floor(Math.random()*6)>>


    That's off by one.  Math.floor(Math.random()*6) returns 05, you'd need to add 1 to that if you wanted a range of 16.

    The reason being that Math.random() returns a real in the range: 0.00.999~9, so multiplying it by 6 yields 0.05.999~94, which when floored yields 05.
  • TheMadExile wrote:

    Jinx wrote:

    Is there a script or a macro for random rolls? Like, I dunno, something simple like <<roll 20 $money>> for a random number between 1 and 20 to add to $money.


    I believe you're using SugarCube, aren't you?

    If so, you can use the random(min, max) utility function.  For example:

    // returns a whole number within the range: 16
    random(1, 6)

    // returns a whole number within the range: 120
    random(1, 20)

    // simulate a 3d6 roll
    random(1, 6) + random(1, 6) + random(1, 6)
    To use that for an amount of coins, let's say, you might do something like these:

    // sets $money to randomly selected amount in the range: 120
    <<set $money to random(1, 20)>>

    // sets $money to randomly selected amount in the range: 1050
    <<set $money to random(10, 50)>>

    Wraithbane wrote:
    <<set $money = Math.floor(Math.random()*6)>>


    That's off by one.  Math.floor(Math.random()*6) returns 05, you'd need to add 1 to that if you wanted a range of 16.

    The reason being that Math.random() returns a real in the range: 0.00.999~9, so multiplying it by 6 yields 0.05.999~94, which when floored yields 05.



    Ah!! Good to know!! Thanks. =)
Sign In or Register to comment.