# Random pairs from an arrayList

**URL:** <https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088>\
**Category:** Coding Questions\
**Created:** [October 31, 2019, 8:48pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088 "2019-10-31T20:48:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [October 31, 2019, 8:48pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088/1 "2019-10-31T20:48:49Z")

</div>

hi all,  
I am a little bit stuck with this simple problem; I have an arrayList of let’s say PVectors and I would like to draw random lines between two different points.( or have any other connection rule ), the number of points is pair.  
no point should be connected to itself and no point should be connected to more than one point.! ![lines](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7a493fe820743473505ad24e21a15f7ea76fc324.jpeg)  
is there any exemple I can consult about this kind of iteration through arrayLists

thanx

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 31, 2019, 9:02pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088/2 "2019-10-31T21:02:57Z")

</div>

You could have a boolean variable to check if its been paired.

Then iterate through the array, for each pvector, and use an if statement to check if “this” and if “!paired”

Then pair.

---

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [October 31, 2019, 9:18pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088/3 "2019-10-31T21:18:58Z")

</div>

thanx paulgoux,

I have tried something similar, but I don’t know where to store the Boolean variable. should I declare an other list ?

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 31, 2019, 9:22pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088/4 "2019-10-31T21:22:07Z")

</div>

If you’re allowed to use classes then just store it in the object, if not create another arraylist with boolean variables tied to the position of your point in the array.

---

<div class="post-metadata">

**Author:** ![benja](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@benja](https://discourse.processing.org/u/benja)\
**Post date:** [October 31, 2019, 9:34pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088/5 "2019-10-31T21:34:10Z")

</div>

Do you have to keep the original oder of your Arraylist?  
Otherwise you can just shuffle it and then connect them pairwise, (1-2, 3-4, 5-6, …).

---

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [October 31, 2019, 9:54pm UTC](https://discourse.processing.org/t/random-pairs-from-an-arraylist/15088/6 "2019-10-31T21:54:40Z")

</div>

thank you Benja,

it’s a really clever and elegant way !
