Howdy, Stranger!

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

Generating random non-numerical values

I'm using Twine 2, SugarCube 2.

How can I set a variable to a random word out of a list?

If I want a variable to be a random number from 1 to10, I do this:
<<set $variable = random(1, 10)>>

But what do I do if, say, I want the variable $pet to be chosen randomly from the following list: cat, dog, fish?

Comments

  • There are several ways, the two easiest of which are the either() function and the <Array>.random() method.

    Assuming that you don't have a preexisting array of pets that you wish to select from, such an assignment might look like the following:
    → Using either()
    <<set $pet to either("cat", "dog", "fish")>>
    
    → Using <Array>.random()
    <<set $pet to ["cat", "dog", "fish"].random()>>
    
  • Thank you!
Sign In or Register to comment.