# Random changing character

**URL:** <https://discourse.processing.org/t/random-changing-character/39887>\
**Category:** Coding Questions\
**Created:** [November 26, 2022, 3:50pm UTC](https://discourse.processing.org/t/random-changing-character/39887 "2022-11-26T15:50:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [November 26, 2022, 3:50pm UTC](https://discourse.processing.org/t/random-changing-character/39887/1 "2022-11-26T15:50:53Z")

</div>

Hi there!

I want yo do a changing random character. I have done this and It starts working

```auto
char c = 33;
c += random(93);

size(500, 500); 
textSize(128); 

text(c, 250, 250); 

      

```

Then, I try to animate It, I want the character yo change as a watch do It, but draw function does not work. Could you please help me?

```auto

char c = 33;
c += random(93);

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

void draw(){
text(c, 250, 250); 
}
      

```

---

<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:** [November 26, 2022, 4:00pm UTC](https://discourse.processing.org/t/random-changing-character/39887/2 "2022-11-26T16:00:53Z")

</div>

> [@humano](#):
>
> `c=33; `  
> `c += random(93);`

Move this into draw() as well

The lines need to be called every time, the lines are not repeated automatically or something (not a _standing order_).

**Full sketch**

```auto
char c = 33;

void setup() {
  size(500, 500);
  background(0); 
  frameRate (4);

  textSize(128);
}

void draw() {
  background(0);   

  c=33; 
  c += random(93);

  text(c, 250, 250);
}

```

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [November 26, 2022, 4:15pm UTC](https://discourse.processing.org/t/random-changing-character/39887/3 "2022-11-26T16:15:20Z")

</div>

Thank you so much!  
And how could I make a different velocity for a every character?

```auto
char c = 33;
char d = 33;

void setup() {
  size(500, 500);
  background(0); 
  frameRate (4);

  textSize(128);
}

void draw() {
  background(0);   

  c=33; 
  c += random(93);
  
  d=33; 
  d += random(93);
  

  text(c, 150, 250);
  text(d, 300,250);
  text(":",250,250);
}

```

---

<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:** [November 26, 2022, 4:20pm UTC](https://discourse.processing.org/t/random-changing-character/39887/4 "2022-11-26T16:20:50Z")

</div>

that’s simple

2 separate timers

- remember that `draw()` runs 60 times per second approx.; so it runs very fast, displays the letter throughout, and only every 1 and 3 seconds changes a letter

**Remark**

To get better initial letters move this

```auto
  c += random(93);
  d += random(93);

```

into setup() - **I haven’t done this.**

**Remarks**

- This works without `frameRate (4);`
- This looks better with 1000 and 3500 as values, because then the 2 letters don’t change at the same time (3000 would be divisible by 1000 (values now)). - **I haven’t done this.**

**Full Sketch**

```auto
char c = 33;
char d = 33;

int timerc, timerd;

void setup() {
  size(500, 500);
  background(0); 
  // frameRate (4);

  textSize(128);
}

void draw() {
  background(0);   

  if (millis() - timerc > 1000) {
    c=33; 
    c += random(93);
    timerc=millis();
  }

  if (millis() - timerd > 3000) {
    d=33; 
    d += random(93);
    timerd=millis();
  }

  text(c, 150, 250);
  text(d, 300, 250);
  text(":", 250, 250);
}

```

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [November 28, 2022, 2:42am UTC](https://discourse.processing.org/t/random-changing-character/39887/5 "2022-11-28T02:42:34Z")

</div>

You could just use modulo with frame count, change the 60 and 180 to what ever you want, i just set them as that for 1 second for c and 3 seconds for d

```auto
if (frameCount % 60 == 0) c += random(93);
if (frameCount % 180 == 0) d += random(93);

```

Much simpler than using variables to track

An example…

```auto
char c = 33;
char d = 33;

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

void draw(){
  background(0);
  
  if (frameCount % 20 == 0) c += random(93);
  if (frameCount % 60 == 0) d += random(93);
  
  fill(255);
  textSize(50);
  text(c, 150, 250);
  text(d, 300, 250);
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 28, 2022, 8:14am UTC](https://discourse.processing.org/t/random-changing-character/39887/6 "2022-11-28T08:14:50Z")

</div>

Hello @humano ,

> [@humano](#):
>
> `c += random(93);`

This should be 94 if you want to include the ~ character

Consider this example (timer not included):

```auto
char c;

void setup()
  {
  size(100, 100);
  frameRate(2);
  }

void draw()
  {
  c = (char) random(33, 127); // Generates a char from 33 to 126
  println(c);
  }

```

Reference:  
[https://processing.org/reference/random\_.html](https://processing.org/reference/random_.html)

Reference to timer used in solution:  
[http://learningprocessing.com/examples/chp10/example-10-04-timer](http://learningprocessing.com/examples/chp10/example-10-04-timer)

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 28, 2022, 8:38am UTC](https://discourse.processing.org/t/random-changing-character/39887/7 "2022-11-28T08:38:35Z")

</div>

> [@Mikey83](#):
>
> `if (frameCount % 20 == 0) c += random(93);`

My favorite timer! I use this often when timing is not critical.

In your example c will go outside of the targeted range of characters 33 to 125 from previous posts.

`:)`

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [November 28, 2022, 9:02am UTC](https://discourse.processing.org/t/random-changing-character/39887/8 "2022-11-28T09:02:59Z")

</div>

Oh, i didnt realise my mistake lol, still, the timer works
