# Simple random string question

**URL:** <https://discourse.processing.org/t/simple-random-string-question/26273>\
**Category:** Coding Questions\
**Created:** [December 15, 2020, 5:06pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273 "2020-12-15T17:06:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SadCSstudent](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@SadCSstudent](https://discourse.processing.org/u/SadCSstudent)\
**Post date:** [December 15, 2020, 5:06pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/1 "2020-12-15T17:06:35Z")

</div>

Hi, I’m trying to make the characters appear on the canvas one the character is pressed but they all seem to appear on the same location and overlap each other.  
int x=int(random(25,450));  
int y=int(random(25,450));  
String word="";  
void setup() {  
size(500, 500);  
}  
void draw(){  
textSize(25);  
fill(0);  
for(int i=0; i\<word.length();i++)  
text(word.charAt(i),x,y);  
}  
void keyPressed(){  
word+=key;  
}

---

<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:** [December 15, 2020, 5:08pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/2 "2020-12-15T17:08:31Z")

</div>

**Random**

> [@SadCSstudent](#):
>
> int x=int(random(25,450));  
> int y=int(random(25,450));

you need to have this in draw; the lines are only ran one and not throughout.

Better is this:

```auto
    x=int(random(25,450));
    y=int(random(25,450));

```

* * *

**How draw() works**

> [@SadCSstudent](#):
>
> for(int i=0; i\<word.length();i++)

another sad truth: `draw()` updates the screen only once at its end and not throughout.

So everything that happens in the for-loop is added up on the screen and then displayed all at once on the screen at the end of draw(). Therefore you cannot see any animation using a for-loop. Bad.

To avoid this: Destroy the for-loop, say `int i=0;` before setup() and use the fact that `draw()` in itself loops automatically: say i++; in draw().

e.g. to slow this down by 10:

```auto
if(frameCount % 10 == 0) 
   i++;

```

**Remark**

…and implement `i<word.length()` …to avoid an error of course…

Chrisir

---

<div class="post-metadata">

**Author:** ![SadCSstudent](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@SadCSstudent](https://discourse.processing.org/u/SadCSstudent)\
**Post date:** [December 15, 2020, 5:23pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/3 "2020-12-15T17:23:50Z")

</div>

thank you for replying again:), I keep getting a string index error so I added the i\<wordlength() aspect but i keep getting the same error. I also never new about that aspect of the draw function!

```auto
int x;
int y;
String word="";
int i=0;
void setup() {
  size(500, 500);
}
void draw() {
  x=int(random(25, 450));
  y=int(random(25, 450));
  textSize(25);
  fill(0);
  text(word.charAt(i), x, y);
  if(frameCount % 10 == 0 && i<word.length()) 
   i++;
  
}
void keyPressed() {
  word+=key;
}

```

---

<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:** [December 15, 2020, 5:25pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/4 "2020-12-15T17:25:17Z")

</div>

Yeah, because word is empty???

---

<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:** [December 15, 2020, 5:29pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/5 "2020-12-15T17:29:28Z")

</div>

APOLOGIES

obviously, this is better:

```auto

int x;
int y;
String wordMy="";
int i=0;

void setup() {
  size(500, 500);
}

void draw() {
  x=int(random(25, 450));
  y=int(random(25, 450));
  textSize(25);
  fill(0);
  if (wordMy.length() > 0) //!!!
    text(wordMy.charAt(i), 
      x, y);
  if ((frameCount % 10 == 0) && (i<wordMy.length()-1)) //!!!!
    i++;
}

void keyPressed() {
  wordMy+=key;
}

```

Also, we could say i=0; when i\>=wordMy.length()-1  
or say i=0; in keyPressed

---

<div class="post-metadata">

**Author:** ![SadCSstudent](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@SadCSstudent](https://discourse.processing.org/u/SadCSstudent)\
**Post date:** [December 15, 2020, 5:53pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/6 "2020-12-15T17:53:57Z")

</div>

thank you for the modification, although they all seem to move randomly around the canvas, and only seem to appear once ,I’m trying to get all the characters appear on random locations on the screen and still(rigid) on the screen. I hope I’m conveying this correctly.

---

<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:** [December 15, 2020, 5:56pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/7 "2020-12-15T17:56:45Z")

</div>

Hm…

OK, draw() starts 60 times per second…

so first, the last letter is drawn again and again.

You could stop the whole process once i \>= word.length() (using if(i\<…) around the lines with random and those with `text()`)

Then only the last letter would be added.

---

<div class="post-metadata">

**Author:** ![SadCSstudent](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@SadCSstudent](https://discourse.processing.org/u/SadCSstudent)\
**Post date:** [December 15, 2020, 6:11pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/8 "2020-12-15T18:11:41Z")

</div>

I’m sorry but I don’t think I understand? for instance if(i\>wordlength() ){?)

---

<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:** [December 15, 2020, 6:21pm UTC](https://discourse.processing.org/t/simple-random-string-question/26273/9 "2020-12-15T18:21:35Z")

</div>

changed this

```auto

int x;
int y;

String wordMy="";

int i=0;

void setup() {
  size(500, 500);
}

void draw() {

  if ( (i<wordMy.length())) {
    //!!!!

    x=int(random(25, 450));
    y=int(random(25, 450));
    textSize(25);
    fill(0);

    if (wordMy.length() > 0) { //!!!
      text(wordMy.charAt(i), 
        x, y);
    }

    i++;
  }
}

void keyPressed() {
  wordMy+=key;
}

```
