Howdy, Stranger!

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

Guaranteeing unique variable?

If I set a variable to a random number, and then later set it to another random number, is there any way to ensure it won't be the same number as the first time?

Comments

  • Not sure how you're doing it but you could use a temporary variable to hold the last number. So you'd have two variables. Let's say, $randomnumber and $tempnumber.

    <<set $tempnumber to $randomnumber>>
    (Do your random calculation)
    <<if $tempnumber == $randomnumber>>
    (If it's still the same number as last time)
  • I am doing something like that at the moment, but how do I tell Twine to keep setting the variable until it's different, however many times that takes?
  • Usually that is done with a loop, but in Twine I think the only way to do iteration is to recursively display the same passage. Or you could use a "while" loop in JavaScript, assuming your random generator is in JavaScript as well.

    There is a way to avoid looping though: pick a number from a range that is 1 smaller than usual and if the new result is greater than or equal to the old result, add one to the new result. That way you can generate any number in the range, except the previous result.

    For example, if you pick a number between 1 and 10 inclusive, the first time use the full range, let's say you get 6. The second time, if the random result is in [1..5], keep it as-is, but if the result is in [6..9], add one so it becomes [7..10].
  • You're talking about a while loop. Theres no easy way to do this in Twine alone, but maybe if you have the <<timedgoto>> macro? I'll do this example with rolling a 6 sided die.
    ::TwistyPassage
    <<set $compare to $diceroll>>
    <<set $diceRoll = (Math.floor(Math.random()*5))+1>>
    <<timedgoto "Checker" 0ms>>

    ::Checker
    <<if $compare == $diceRoll>>
    <<timedgoto "TwistyPassage" 0ms>>
    <<else>>
    <<timedgoto "Results" 0ms>>
    <<endif>>

    ::Results
    Your result is <<print $diceRoll>>
    This isn't a copy paste solution because I havent tested it, but the idea is that we check on a different passage to see if they're the same, and if they are, we go back to the roller page which performed the function all over again.

    EDIT: Never mind me and my retarded butt. Apparently you don't need timedgoto! I didn't know you could run a page by wrapping a passage name in <<>>. Holy crap.
  • Nice! Two smart solutions. Thanks! :)
  • CoraBlue wrote:
    EDIT: Never mind me and my retarded butt. Apparently you don't need timedgoto! I didn't know you could run a page by wrapping a passage name in <<>>. Holy crap.


    And you just blew mine.  Thanks!  ;D
Sign In or Register to comment.