# How to create randomly assigned array?

**URL:** <https://discourse.processing.org/t/how-to-create-randomly-assigned-array/10744>\
**Category:** Coding Questions\
**Created:** [April 29, 2019, 7:58am UTC](https://discourse.processing.org/t/how-to-create-randomly-assigned-array/10744 "2019-04-29T07:58:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmarty](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@danmarty](https://discourse.processing.org/u/danmarty)\
**Post date:** [April 29, 2019, 7:58am UTC](https://discourse.processing.org/t/how-to-create-randomly-assigned-array/10744/1 "2019-04-29T07:58:00Z")

</div>

Hi everyone,  
I’m still very new to processing, and I want to be able to create an array (or class?) of 7 different floor types I have made out of rectangles. Below is the code for these different floor types, in the state I have currently got them. I want to have a section of code that can generate one of the floor types, randomly chosen, at a set interval indefinitely. Any pointers towards how to achieve this would be very much appreciated!

```auto
int yPos1 = 0;
int yPos2 = 125;
int yPos3 = 250;
int yPos4 = 375;
int yPos5 = 500;
int yPos6 = 625;
int yPos7 = 750;
float speed = 2.5, direction = 1;

void setup() {
  size (800, 800);
  frameRate(60);
}

void draw()
{

  clear(); 
  background (129, 209, 245);
  fill (245, 181, 62);
  rect(0, yPos1, 300, 20);
  rect (450, yPos1, 800, 20);
  yPos1 += speed * direction;

  fill (245, 181, 62);
  rect(150, yPos2, 450, 20);
  yPos2 += speed * direction;

  fill (245, 181, 62);
  rect(300, yPos3, 400, 20);
  rect(0, yPos3, 150, 20);
  yPos3 += speed * direction;

  fill (245, 181, 62);
  rect(200, yPos4, 600, 20);
  yPos4 += speed * direction;

  fill (245, 181, 62);
  rect(0, yPos5, 500, 20);
  rect(650, yPos5, 150, 20);
  yPos5 += speed * direction;

  fill (245, 181, 62);
  rect(0, yPos6, 200, 20);
  rect (325, yPos6, 125, 20);
  rect (600, yPos6, 200, 20);
  yPos6 += speed * direction;

  fill (245, 181, 62);
  rect(0, yPos7, 600, 20);
  yPos7 += speed * direction;
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [April 29, 2019, 9:00am UTC](https://discourse.processing.org/t/how-to-create-randomly-assigned-array/10744/2 "2019-04-29T09:00:17Z")

</div>

Hi there,

First of all, you can format your code by pressing `Ctrl+T` in the Processing IDE code editor and use the `</>` button when you edit a message on the forum.

Then there’s few things to change before trying to solve your problem. You are repeating yourself too much :

- You can set the `fill` color once (because it’s going to stay the same)
- Since you have the same objects (your floors) but with different properties, it’s useful to create a specific class so your code is modular ([https://processing.org/tutorials/objects/](https://processing.org/tutorials/objects/))

For example, the structure could be :

```auto
class Floor{
    int posX, posY, sizeX, sizeY;
    Floor(int posX, int posY, int sizeX, int sizeY){/*...*/}
    void display(){/*...*/}
    void update(){/*...*/}
}

```

---

<div class="post-metadata">

**Author:** ![danmarty](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@danmarty](https://discourse.processing.org/u/danmarty)\
**Post date:** [April 29, 2019, 9:06am UTC](https://discourse.processing.org/t/how-to-create-randomly-assigned-array/10744/3 "2019-04-29T09:06:04Z")

</div>

Awesome, thanks for the help!

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [April 29, 2019, 1:27pm UTC](https://discourse.processing.org/t/how-to-create-randomly-assigned-array/10744/4 "2019-04-29T13:27:56Z")

</div>

Many ways to approach this. some points should care about

- you can use `ArrayList` of the floor class based on josephh post to keep track the floor objects like what @josephh has posted. see [https://processing.org/reference/ArrayList.html](https://processing.org/reference/ArrayList.html)
- on the certain condition, like the floor get to the bottom of the screen, remove that floor and **generate** a new floor and push to the `ArrayList` or you can just reset that floor to the initial condition with random properties (position and size or something) without being removed
- see [https://processing.org/reference/random\_.html](https://processing.org/reference/random_.html) to randomize thing
