Choose random multiple entries from a StringList

Hello,

Yesterday I was trying to pick three random entries from a StringList when you pressed a button. Ideally, the entry shouldn’t be repeated. I decided to shuffle() it once at the beggining, with three int which would increase until it reached the end of the StringList. Here it is the code I ended up coming up with:

At the beggining:

participant.shuffle();
        name1 = 0;
        name2 = 1;
        name3 = 2;

While the text that appears on screen says:

text ("participants " + participant.get(name1) + ", "+ participant.get(name2) + " and " + participant.get(name3), 630,250);

and when you press a button to get a new set of entries:

  void display(){
      if (mouseX > width-x && mouseX < width-(x-botx) && mouseY > height-y && mouseY < height-(y-boty) && mousePressed == true){
        if (name1 < participant.size()-3) {
        name1 = name3 +1;
        name2 = name1 +1;
        name3 = name2 +1;
        mousePressed = false;}
        else {
        participant.shuffle();
        name1 = 0;
        name2 = 1;
        name3 = 2;
        mousePressed = false;}
       }

but it gets the ArrayIndexOutOfBoundsException: Array index out of range: 4 (here it is 4, but it really doesn’t matter as the StringList is created by user input).

Right now, the code works because I decided to get rid of the increase of the int and I just shuffle every time a new set of entries needs to happen. However, it bothers me because it tends to get repeated quite often while I’d like all entries to come up eventually. I tried using for() and random() which creates even worse results.

I hope I made myself clear and you find a smarter way to approach it. Thanks so much for reading!