Home > Blockchain >  How to make a list of words?
How to make a list of words?

Time:08-06

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?

CodePudding user response:

import random
l = ['hello', 'world']
element = random.choice(l)

However, I would recommend watching a python tutorial or googling your question before asking it on stack overflow.

CodePudding user response:

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