How to make a list of words?

Viewed 40

I need to make a list containing words or phrases, then from this list I would like to randomly select an element and pass it to a variable. How can I do this?

1 Answers

You can do it with the Random.nextInt([int? max]) method passing the length of the list as its max argument:

const list = ['a', 'b', 'c'];
final res = list[Random().nextInt(list.length)];

When you pass the length of the sequence as the nextInt max argument, Random will return an integer value that is in a range between 0 (inclusive) and list length (exclusive).

Related