Howdy, Stranger!

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

Random number roll with 3 outcomes

Hi all,

I've been using the posts on this forum to piece together a Twine game (I'm not a coder, as will become obvious...) I'm making to use some of my music/audio in.

I have a problem where I'm trying to generate a random number between 1 and 10, with 3 possible outcomes.
I found the following post which I thought would solve the problem:
http://twinery.org/forum/index.php/topic,1490.msg2655.html#msg2655

And so set about putting it to effect.  Here's the code I am using:

<<set $number = Math.floor((Math.random()*10)+1)>>
<<if $number lte 1>>
<<goto "misfire">>
<<elseif $number gte 2 or $number lte 3>>
<<goto "noeffect1">>
<<elseif $number gte 4>>
<<goto "area1bearingsuccess">>
<<endif>>

The intent is to have a 10% chance of the first outcome, a 20% chance of the second, and a 70% chance of the third; with the story progressing to the relevant passage.
The problem is that it seems to get stuck choosing the 2nd option (i.e. the number 2 or 3 has been returned). 

I'm guessing this is a straight up simple mistake I'm making, but any pointers would be very much appreciated.

Thanks,
Mickey


Comments

  • You're using the wrong logic for that case.  Try this:

    <<elseif $number gte 2 and $number lte 3>>
    i.e. Exchange the or operator for the and operator.

    Personally, I'd probably simply that logic a bit, like so:

    <<set $number = Math.floor((Math.random()*10)+1)>>
    <<if $number gte 4>>
    <<goto "area1bearingsuccess">>
    <<elseif $number gte 2>>
    <<goto "noeffect1">>
    <<else>>
    <<goto "misfire">>
    <<endif>>
    That's more of a personal preference thing though.
  • Hey TheMadExile,

    Thanks a lot for your suggestion.  I ended up going with your revised code and it works a treat! 
    Much appreciated :-)

  • Hmmm. goto isn't in the wiki.

    If it was and it took a variable for its destination...
    <<set $dest to either("misfire","noeffect1","noeffect1","area1bearingsuccess","area1bearingsuccess","area1bearingsuccess","area1bearingsuccess","area1bearingsuccess","area1bearingsuccess","area1bearingsuccess")>>
    <<goto $dest>>
Sign In or Register to comment.