How to set a random name?

Hi everyone,
I’m new in coding and programming in general.
So this is my first book game and i want to give a random name to a city.
I tried in harlowe first and the code was like this:

(set: $cities to (a: “Trassa”, “Delo”, “Manesh”, “Laaur”, “Grond”))
(set: $city = $cities’s (random: 1,5))

But because harlowe is quite limited instead of sugarcube I moved there,
so now i don’t know how to transport this part of code.

thank you for your help!

<<set $cities to ["Trassa", "Delo", "Manesh", "Laaur", "Grond"]>>
<<set $city to either($cities)>>

A list in square brackets makes an array, and then either picks a random element of an array.

2 Likes

Thank you so much!

Additional to Josh Grams advice…

There are a couple of other methods you can use to obtain a single random element from a pre-defined Array llike

1: the <Array>.random() method, which returns a copy of a random selected element.

<<set _city to $cities.random()>>

In the above example, the $cities variable will still contain the five city names, even though the _city variable contains one of those names.

2: the <Array>.pluck() method, which returns a random selected element, that has been removed from the Array.

<<set _city to $cities.pluck()>>

In the above example, one of the city names is now stored in the _city variable, and the $cities variable now only contains four city names.

3 Likes