# Homework Help! Getting random string from array

**URL:** <https://discourse.processing.org/t/homework-help-getting-random-string-from-array/21420>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [May 30, 2020, 2:16am UTC](https://discourse.processing.org/t/homework-help-getting-random-string-from-array/21420 "2020-05-30T02:16:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoshuaStark](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@JoshuaStark](https://discourse.processing.org/u/JoshuaStark)\
**Post date:** [May 30, 2020, 2:16am UTC](https://discourse.processing.org/t/homework-help-getting-random-string-from-array/21420/1 "2020-05-30T02:16:42Z")

</div>

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.

```auto
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
  
    
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 30, 2020, 6:23am UTC](https://discourse.processing.org/t/homework-help-getting-random-string-from-array/21420/2 "2020-05-30T06:23:14Z")

</div>

See website, Tutorials, then Arrays

Use command random()

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

```auto
greetingList ....................

```
