# How to make something inside a loop happen once?

**URL:** <https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807>\
**Category:** Coding Questions\
**Created:** [February 28, 2019, 3:36pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807 "2019-02-28T15:36:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobo](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@jacobo](https://discourse.processing.org/u/jacobo)\
**Post date:** [February 28, 2019, 3:36pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/1 "2019-02-28T15:36:59Z")

</div>

Hey guys.

In this code I’d like the words to be randomly generated just once and not over and over again, while still keeping it bouncing from left to right.

I’m new at processing so help is much appreciated!

```auto
float textX; // x position
float xspeed = 0.15; 
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"}; // string med de 3 sætninger
PFont font1;

void setup() {
  println(words.length);
  size (700, 1000);
  font1 = loadFont ("Akkurat-Bold-18.vlw");
  textAlign(LEFT);
  textX = 0;
  frameRate(20);
}

void draw() {
  background (0);
    textFont(font1, 30); 
  for (int y = 25; y < height; y = y + 30) { 
      fill(255);
      text(words [int(random(words.length))], textX, y); 

      textX = textX + xspeed; 
      

      
      if (textX > width) {
        xspeed = -0.15; 
      }
      
      if (textX < 0) { 
        xspeed = 0.15;
      }
    }
}

```

---

<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:** [February 28, 2019, 4:34pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/2 "2019-02-28T16:34:36Z")

</div>

> [@jacobo](#):
>
> int(random(words.length))

store this in a Variable in setup()

use this variable in draw()

Declare that int variable before setup ()

Chrisir

---

<div class="post-metadata">

**Author:** ![jacobo](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@jacobo](https://discourse.processing.org/u/jacobo)\
**Post date:** [February 28, 2019, 4:54pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/3 "2019-02-28T16:54:24Z")

</div>

Hi Chris

I can’t figure it out

I did this:

–

before setup:  
int a;

in setup():  
a = words.length;

in draw():  
text(words (random(a)), textX, y);

–

What am I missing here?

---

<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:** [February 28, 2019, 6:42pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/4 "2019-02-28T18:42:34Z")

</div>

```auto

// string med de 3 sætninger
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
// its index 
int a;
// text position X
int textX=0;

void setup() {
  size(800, 700);
  a = int(random(words.length));
  background(0);
}

void draw() {
  background(0);
  text(words[a], textX, 100);
  textX = textX + 5;
}

```

when you are done with one word (it left the screen) restart `textX` and `a`

---

<div class="post-metadata">

**Author:** ![jacobo](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@jacobo](https://discourse.processing.org/u/jacobo)\
**Post date:** [March 1, 2019, 8:56am UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/5 "2019-03-01T08:56:00Z")

</div>

Hi Chris

I’m getting this weird effect instead

```auto
//float textX; // x position
float xspeed = 0.15; // fart som den bevæger sig på x-aksen
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"}; // string med de 3 sætninger
PFont font1;
int a;
int textX=0;

void setup() {
  println(words.length);
  size (700, 1000);
  font1 = loadFont ("Akkurat-Bold-18.vlw");
  textAlign(LEFT);
  textX = 0;
  frameRate(20);
fill( random(255), random(255), random(255), random(255)); 
a = int(random(words.length));
  

}

void draw() {
  
  background (0);
    textFont(font1, 30); // loader skrifttype
  for (int y = 25; y < height; y = y + 30) { // for loop på y-aksen
      text(words [a], textX, y); // String er genereret random
      textX = textX +5;

      //textX = textX + xspeed; // bevæg med xspeed til højre hvert loop
      
// bounce frem og tilbage
      
      if (textX > width) {
        xspeed = -0.15; //hvis tekst når width skal den gå modsat vej
      }
      
      if (textX < 0) { // hvis tekst når 0 skal den gå tilbage igen
        xspeed = 0.15;
      }
    }
//

}

```

---

<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 1, 2019, 9:09am UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/6 "2019-03-01T09:09:10Z")

</div>

Can you please describe the effect

---

<div class="post-metadata">

**Author:** ![jacobo](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@jacobo](https://discourse.processing.org/u/jacobo)\
**Post date:** [March 1, 2019, 9:22am UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/7 "2019-03-01T09:22:26Z")

</div>

I want the code to randomly select words from the string just _once_ in the start, so it looks like this:

vinterjazz  
01-24 feb. 2019  
vinterjazz  
www.jazz.dk  
www.jazz.dk  
01-24 feb. 2019  
vinterjazz  
etc

right now it keeps on generating new ones

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [March 1, 2019, 4:21pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/8 "2019-03-01T16:21:46Z")

</div>

Hi @Jacobo,

For a cleaner approach I would suggest:

- to divide the height of your canvas by the desired number of lines.
- to make a list of all the words picked randomly in `setup()`
- that list will have he same size as the desired number of lines
- to compute the length of the longest word for a cleaner edge detection

Regarding your issue, you just have to multiply the speed by `-1` when the `location` of your text reaches an edge.

Here’s an example in Python Mode.

```auto
nLines, speed, location = 32, 1.8, 0
words = ["vinterjazz", "01-24 feb. 2019", "www.jazz.dk"]
wordsList = [words[int(random(len(words)))] for i in range(nLines)]

def setup():
    size(700, 1000, P2D)
    fill(255)
    
    global step, longestWord
    step = height / nLines
    longestWord = max([textWidth(w) for w in words])
    
def draw():
    background(0)
    
    global location, speed
    location += speed
    
    for i in range(nLines): text(wordsList[i], location, (i+1) * step)
    if location + longestWord >= width or location <= 0: speed *= -1

```

And here below my failed attempt in Java, let’s hope someone will find some time to fix it.

```auto
float location = 0;
float speed = 1.8;
int nLines = 32;
float step = 1000 / nLines;
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
float longestWord = 0.0;
String [] wordsList;

void setup() {
  size(700, 1000, P2D);
  fill(255);
 
  
  for (int i = 0; i < words.length; i++) { 
    longestWord = max(longestWord, textWidth(words[i]));
  }
  
  for (int i = 0; i < nLines; i++) { 
    wordsList[i] = words[int(random(words.length))]; 
  }
}

void draw() {
  background(0);
  
  location += speed;
  
  for (int i = 0; i < nLines; i++) {
    text(wordsList[i], location, (i+1) * step);
  }
  
  if (location + longestWord >= width || location <= 0) {
    speed *= -1;
  }
}

```

---

<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 1, 2019, 8:33pm UTC](https://discourse.processing.org/t/how-to-make-something-inside-a-loop-happen-once/8807/9 "2019-03-01T20:33:23Z")

</div>

here is a solution that has one line only that’s chosen once and for all (not what you want, I know)

```auto

//float textX; // x position
float xspeed = 2.15; // fart som den bevæger sig på x-aksen
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"}; // string med de 3 sætninger
PFont font1;
int a;
float textX=10;

void setup() {
  println(words.length);
  size (700, 1000);
  font1 = createFont ("Akkurat-Bold-18.vlw", 30);
  textAlign(LEFT);
  textX = 0;
  frameRate(20);
  fill( random(100, 255), random(100, 255), random(100, 255) ); 
  a = int(random(words.length));
}

void draw() {
  background (0);

  textFont(font1, 30); // loader skrifttype

  text(words[a], textX, 200); // String er genereret random

  textX = textX + xspeed; // bevæg med xspeed til højre hvert loop

  // bounce frem og tilbage
  if (textX > width-textWidth(words[a])) {
    xspeed = -abs(xspeed); //hvis tekst når width skal den gå modsat vej
  }

  if (textX < 0) { // hvis tekst når 0 skal den gå tilbage igen
    xspeed = abs(xspeed);
  }

  //
}

```

here a version like from solub avbove in java

```auto

float location = 0;
float speed = 1.8;
int nLines = 32;
float step = 1000 / nLines;
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
float longestWord = 0.0;
String [] wordsList = new String[nLines];

void setup() {
  size(700, 1000, P2D);
  fill(255);

  for (int i = 0; i < words.length; i++) { 
    longestWord = max(longestWord, textWidth(words[i]));
  }

  for (int i = 0; i < nLines; i++) { 
    wordsList[i] = words[int(random(words.length))];
  }
}// function 

void draw() {
  background(0);

  location += speed;

  for (int i = 0; i < nLines; i++) {
    text(wordsList[i], location, (i+1) * step);
  }

  if (location + longestWord >= width || location <= 0) {
    speed *= -1;
  }
}// function

```

similar but with different colors and different x pos (and speed) for each line:

```auto
int nLines = 32;
float step = 1000 / nLines;
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
float longestWord = 0.0;

String [] wordsList = new String[nLines];
float[] location = new float[nLines];
float[] speed = new float[nLines]; // 1.8
color[] col = new color[nLines]; 

void setup() {
  size(600, 1000, P2D);

  for (int i = 0; i < words.length; i++) { 
    longestWord = max(longestWord, textWidth(words[i]));
  }

  for (int i = 0; i < nLines; i++) { 
    wordsList[i] = words[int(random(words.length))];
    location[i] = i*3.4;
    speed[i] = 1.8;
    col[i] = color ( random(100, 255), random(100, 255), random(100, 255));
  }
}// function 

void draw() {
  background(0);

  for (int i = 0; i < nLines; i++) {

    fill ( col [i] ); 
    text(wordsList[i], 
      location[i], (i+1) * step);

    location[i] += speed[i];

    if (location[i] + longestWord >= width || location[i] <= 0) {
      speed[i] *= -1;
    }
  }//for
}// function

```
