Random changing character

Hi there!

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

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?


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

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

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

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

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);
}

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

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);
}

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

  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

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);
}

2 Likes

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

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

Much simpler than using variables to track

An example…

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);
}
1 Like

Hello @humano ,

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

Consider this example (timer not included):

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

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

:)

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.

:)

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

1 Like