Homework Help! Getting random string from array

I need to get a random greeting from my array using their index and display that random greeting when I press the button and it needs to be done in my greeting function.

import controlP5.*;

ControlP5 cp5;

controlP5.Button greetingButton;

String[] greetingList = {"hello", "bonjour", "encantado", "ohayo", "ciao", "namaste", "ni hao", "hei", "guten tag", "buongiorno", "salam", "hej", "salut", "hola"};
String currentGreeting = "hello";


void setup()
{
  size(400, 400);
  
  cp5 = new ControlP5(this);
  
  greetingButton = cp5.addButton("greeting")
                  .setPosition(100, 200)
                  .setSize(200, 100)
                  .setCaptionLabel("new greeting?")
                  .setColorBackground(255)
                  ;
                   
 

void draw()
{
  background(74, 158, 84);
  
  text("hi", 100, 150);
 
}


void greeting()
{
  // Choose a random index from the possible indexes in the greeting array
  // Use the index to access that greeting out of the array
  
    
}

See website, Tutorials, then Arrays

Use command random()

int index = int(random( greetingList.length ));

greetingList ....................
1 Like