How do I pull a random string from an array?

Hello, I've been messing with this for about an hour but I know I'm doing some small thing incorrectly that's making it not work. I did it fine in rpgmaker vx, so maybe it's my syntax or something.

I've assigned a bunch of strings to an array which is in a variable like this

Script:$gameVariables.setValue(20, ["A", "E", "I", "O", "U"]);

so from there, how would I go about pulling a random string from that array and then displaying it? I can of course display the ENTIRE variable contents in an event by using: \V[20], but that shows everything in there and I want to just show one string at random. I tried using $gameMessage.add("\V[20]") but then it just displays the string "\V[20]".

I know I'm missing something relatively simple, so any help would be greatly appreciated.
You can try this
var pool = ["A", "E", "I", "O", "U"];
$gameVariables.setValue(20, pool[Math.floor(Math.random() * pool.length)]
Then just use \v[20] in text box to display the string.
To retrieve a random value from a game variable array you could do something like this:

Code:
$gameVariables.value(20)[Math.randomInt($gameVariables.value(20).length)]

The easiest way of displaying this information is just to save it to a new variable and using the \v[id] method. To do this you would run the script

Code:
$gameVariables.setValue(21, $gameVariables.value(20)[Math.randomInt($gameVariables.value(20).length)]

From there just use \v[21] and you're good to go. 🙂
You can try this
var pool = ["A", "E", "I", "O", "U"];
$gameVariables.setValue(20, pool[Math.floor(Math.random() * pool.length)]
Then just use \v[20] in text box to display the string.

Thank you for your advice. Im pretty sure I copied your code exactly but get a syntax error that says missing ) after argument list.
@starbrother Add another parentheses on the end of the second line :

Code:
var pool = ["A", "E", "I", "O", "U"];
$gameVariables.setValue(20, pool[Math.floor(Math.random() * pool.length)]);
Ah, thanks. I tried putting it myself but I added it in the wrong spot. The code itself works now but when I have the contents of V[20] displayed it just shows as a zero, when it should be one of the strings that would ostensibly be in the variable. Still, I feel like I'm getting closer to it working.
@starbrother If you are using the above array -- ["A", "E", "I", "O", "U"] -- perhaps that is the 'O' that looks like a '0'?
@starbrother If you are using the above array -- ["A", "E", "I", "O", "U"] -- perhaps that is the 'O' that looks like a '0'?

Just to make sure that wasn't it, I changed the variable to hold a,e,i,y,u and still get a number 0. I can take a pic if it would help at all.
@starbrother Hmm, the code certainly appears to function correctly on my end. Is there previous code snippets somewhere that might be setting the value to zero? If the code is being used within in an event, perhaps copying and pasting the contents of that event might be helpful (click in the 'Contents' area of the Event Editor, press CTRL+A to select all, and then right-click to 'Copy as Text').
@starbrother Hmm, the code certainly appears to function correctly on my end. Is there previous code snippets somewhere that might be setting the value to zero? If the code is being used within in an event, perhaps copying and pasting the contents of that event might be helpful (click in the 'Contents' area of the Event Editor, press CTRL+A to select all, and then right-click to 'Copy as Text').

It works, it was as you had said, thee was another bit of code that was setting the value to 0. Thank you so much, I might end up pulling the strings from a text file to make it not hang so much since there are quite a few options, but this is a great start, I can't thank you guys enough! This thread can be closed down now.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-pull-a-random-string-from-an-array.113758/