# Repeating and randomizing string arrays

**URL:** <https://discourse.processing.org/t/repeating-and-randomizing-string-arrays/10811>\
**Category:** Libraries\
**Created:** [May 1, 2019, 3:12pm UTC](https://discourse.processing.org/t/repeating-and-randomizing-string-arrays/10811 "2019-05-01T15:12:52Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 9, 2019, 3:57am UTC](https://discourse.processing.org/t/repeating-and-randomizing-string-arrays/10811/6 "2019-05-09T03:57:21Z")

</div>

> [@AwreetusAwrightus](#):
>
> I’m not sure this a case-specific compared to those you shown me,

The **shuffle()** function from the 1st link I’ve posted is for arrays of datatype `int` only:

```auto
@SafeVarargs final int[] shuffle(final int... arr) {
  if (arr == null) return null;
 
  int idx = arr.length;
 
  while (idx > 1) { 
    final int rnd = (int) random(idx--), tmp = arr[idx];
    arr[idx] = arr[rnd];
    arr[rnd] = tmp;
  }
 
  return arr;
}

```

For object-typed arrays, we need to convert it, like this:

```auto
@SafeVarargs final <T> T[] shuffle(final T... arr) {
  if (arr == null) return null;

  int idx = arr.length;

  while (idx > 1) { 
    final int rnd = (int) random(idx--);
    final T tmp = arr[idx];

    arr[idx] = arr[rnd];
    arr[rnd] = tmp;
  }

  return arr;
}

```

Alternatively, as the 2nd posted link advises, we can use the not-so-new Processing containers.

For strings, we use a StringList: [StringList / Reference / Processing.org](https://Processing.org/reference/StringList.html)

So we can have access to its methods StringList::**shuffle()** & StringList::**array()**:

1. [shuffle() / Reference / Processing.org](http://Processing.org/reference/StringList_shuffle_.html)
2. [array() / Reference / Processing.org](http://Processing.org/reference/StringList_array_.html)

---

_[View the full topic](https://discourse.processing.org/t/repeating-and-randomizing-string-arrays/10811)._
