# How to store random coordinates?

**URL:** <https://discourse.processing.org/t/how-to-store-random-coordinates/28235>\
**Category:** Beginners\
**Created:** [March 3, 2021, 10:10pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235 "2021-03-03T22:10:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 3, 2021, 10:10pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/1 "2021-03-03T22:10:16Z")

</div>

I would like to arrange some spheres randomly in space. To do that I run the following:

```auto
import peasy.*;
PeasyCam cam;

void setup(){
  size(1300, 1300, P3D);
  cam = new PeasyCam(this, 200);

  randomSeed(0);
  
  float posx=random(-50, 50);
  float posy=random(-50, 50);
  float posz=random(-50, 50);

}

void draw() {
  background(0);

  for (int i=0; i < numRows; i ++) { // where numRows is defined somewhere and are some values that I loop over to determine the size of the sphere
    pushMatrix();
    lights();
    translate(posx, posy, posz);
    sphere(50); // in my code, the size of the sphere is determined by some imported values
    popMatrix();
  }

```

But I don’t know to store the values in an array so I can access them in my for loop one by one and determine a random position. Is there a remedy? Thank you!

---

<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:** [March 3, 2021, 10:15pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/2 "2021-03-03T22:15:11Z")

</div>

Look at ArrayList and set one up to type PVector  
(before `setup()`)

Then in a for loop make PVectors and add them to the arraylist  
(All in `setup()`)

In draw() read and show arraylist

See reference ArrayList

**Example**

```auto

import peasy.*;
PeasyCam cam;

ArrayList<PVector> list = new ArrayList();  

void setup() {
  size(1300, 1300, P3D);
  cam = new PeasyCam(this, 200);

  // randomSeed(0);

  for (int i=0; i<5; i++) {
    float posx=random(0, 150);
    float posy=random(0, 150);
    float posz=random(-450, 0);
    list.add(new PVector(posx, posy, posz));
  }
}

void draw() {
  background(0);
  lights();

  noStroke(); 
  fill(255, 0, 0); 

  for (PVector pv : list) {
    pushMatrix();
    translate(pv.x, pv.y, pv.z);
    sphere(14); 
    popMatrix();
  }
}

```

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 3, 2021, 11:13pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/3 "2021-03-03T23:13:09Z")

</div>

Awesome, thanks!

One follow-up question:

In this for-loop

```auto
for (PVector pv : list) {
    pushMatrix();
    translate(pv.x, pv.y, pv.z);
    sphere(14); 
    popMatrix();
}

```

How could I use the loop index (pv) to extract elements from an IntList that I’ve defined outside of the loop?

When I run the following

```auto
float var = map(myIntList.get(pv), 0, 10000000000, 0, 100);

```

I get the error: The function “get()” expects parameters like: “get(int)”

Thank you!

---

<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:** [March 3, 2021, 11:16pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/4 "2021-03-03T23:16:52Z")

</div>

When you want it in the same for loop and both lists have the same length use a conventional for loop with i and use get(i) on both lists

---

<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:** [March 3, 2021, 11:17pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/5 "2021-03-03T23:17:26Z")

</div>

Alternatively you can use an int i and say i++; in the for loop

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 3, 2021, 11:21pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/6 "2021-03-03T23:21:16Z")

</div>

So I’m using the conventional for loop but when I run the following

```auto
    translate(list.x, list.y, list.z);

```

I get the error “The global variable “x” does not exist” (same for y and z). Why is that?

---

<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:** [March 3, 2021, 11:22pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/7 "2021-03-03T23:22:38Z")

</div>

> [@tea\_tree](#):
>
> `list.x,`

say list.get(i).x

For all 3 elements

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 3, 2021, 11:24pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/8 "2021-03-03T23:24:35Z")

</div>

It’s aliiiive! Thank you 🙂

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 4, 2021, 4:24pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/9 "2021-03-04T16:24:27Z")

</div>

A bit of help with ArrayLists;

```auto
ArrayList<Integer> abc = new ArrayList<Integer>();
abc.add(123); 
println(abc.get(0)*2); //it acts the same as int value = 123;

```

essentially `ArrayListName.get(<id>) ` acts the same as varriableName.

It helps if you want to do something like:

```auto
ArrayList<ArrayList<Float>> info = new ArrayList<ArrayList<Float>>();

```

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 4, 2021, 5:13pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/10 "2021-03-04T17:13:45Z")

</div>

Thanks.

If I could just ask a follow-up question. I would like to be able to retrieve a name for each of my x, y, z coordinates.

Something like

```auto
String name = myTable.getString(i, 0);
float posx=random(0, 150);
float posy=random(0, 150);
float posz=random(0, 150);

```

I’d like to store these in some sort of array so that I can access the coordinates together with the name. But I see that Arrays and ArrayLists are type specific. Is there a datatype that is not type specific? Or is there a smarter way to access the name-coordinate pairs later in my code?

Thanks!

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 4, 2021, 5:37pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/11 "2021-03-04T17:37:50Z")

</div>

You can store 3 coordiantes in the PVector class. It is in the format `new PVector(x,y,z)`.

To access it use:

```auto
float x = 123, y = 321, z = -333;
PVector pos = new PVector(x,y,z);
//instead of sphere(x,y,z) use sphere(pos.x,pos.y,pos.z);
//or in ArrayList form
ArrayList<PVector> list = new ArrayList<PVector>();
list.add(new PVector(x,y,z));
square(list.get(i).x,list.get(i).y,sphere.get(i).z);

```

In the PVector class, you can essentially store 3 float values.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 4, 2021, 5:39pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/12 "2021-03-04T17:39:24Z")

</div>

The only minor “issue” with PVectors is that you cannot use

```auto
PVector pos = 1,5,-4; //this will not work
//you have to use:
PVector pos = new PVector(1,5,-4);

```

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 4, 2021, 5:42pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/13 "2021-03-04T17:42:13Z")

</div>

I see, I understand the PVector part. What if I wanted to link a name to each PVector? So that it kind of looks like this

```auto
name1 posx1 posy1 posz1
name2 posx2 posy2 posz2
name3 posx3 posy3 posz3
...

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 4, 2021, 5:59pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/14 "2021-03-04T17:59:31Z")

</div>

I don’t fully understand, but…

```auto
PVector name1 = new PVector(posx1,posy1,posz1);
PVector name2 = new PVector(posx2,posy2,post2);
PVector name3 = new PVector(posx3,posy3,posz3);

//to get varriables use
float posx1 = name1.x;
float posy1 = name1.y;
float posz1 = name1.z;

float posx2 = name2.x;
float posy2 = name2.y;
float posz2 = name2.z;

float posx3 = name3.x;
float posy3 = name3.y;
float posz3 = name3.z

```

---

<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:** [March 4, 2021, 8:00pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/15 "2021-03-04T20:00:27Z")

</div>

You need to make a class

```auto
class MyPoint {
   PVector pvPoint; 
   String namePoint; 
}

```

Then the Arraylist ist of type MyPoint  
`ArrayList<MyPoint > list = new ....`

Then it’s e.g. `println(list.get(3).namePoint);`

**see website | tutorials | objects to learn more**

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [March 4, 2021, 8:09pm UTC](https://discourse.processing.org/t/how-to-store-random-coordinates/28235/16 "2021-03-04T20:09:25Z")

</div>

Awesome, that’s exactly what I was looking for @Chrisir. Thanks!
