Home > Software engineering >  Random character String processing
Random character String processing

Time:12-27

So, I want to make a vertical line with characters (letters) in order to create something similiar to the Matrix effect. I started with a number string, just to see if it worked and it did

String character = str (floor (random(10)));
this.letter = character;

Now I want it to have letters instead of numbers, but I don't now how to make it generate randomly. I tried with char and string, but it shows more than one letter

character  = char (int(random(65, 65 24)));

What am I missing?

CodePudding user response:

char (int(random(65, 65 24))); is indeed the right way to get a random letter.

character = char (int(random(65, 65 24))); means you append/concatenate one letter at a time therefore your character variable will increase with a new char each iteration.

character = char (int(random(65, 65 24))); would replace the current char with a new random one each iteration.

If you want to make vertical text you can use the new line character (\n). Unfortunately you can't easily swap a character out with the String class, but with a bit of substring() and concatenation you can simulate something similar. (The StringBuilder java class would make character swapping easier). Here's a commented example using String:

// full string of letters
String letters = "";
// maximum letters in a string
int maxLetters = 12;
// which character to swap
int charIndex = 0;

void setup(){
  size(300, 300);
  fill(0, 192, 0);
  textAlign(CENTER);
  textFont(createFont("Courier New", 12), 12);
  // populate the string
  for(int i = 0 ; i < maxLetters; i  ){
    letters  = getRandomLetter()   "\n";
  }
}

void draw(){
  // pick random char
  char randomChar = getRandomLetter();
  // replace existing characters
  letters = setCharAt(letters, randomChar, charIndex);
  // increment the char index by 2 to include \n
  // use the modulo operator to loop back to 0
  charIndex = (charIndex   2) % letters.length();
  
  // render the text
  background(0);
  text(letters, width * 0.5, height * 0.25);
}

// returns a random a-z char
char getRandomLetter(){
  return char (int(random(65, 65 24)));
}

// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex){
  return myString.substring(0, myCharIndex)   myNewChar   myString.substring(myCharIndex   1); 
}

CodePudding user response:

Alternate method which uses an IntList to hold integers used to create letters. Numbers and corresponding letters in list are sequentially changed with each draw() cycle; frameRate may be slowed to 1 to see changes.

IntList  charNum;
int y = 0;
int index = 0;

void display () {
  background(0);
  y = 30;
  for (int i = 0; i < charNum.size(); i  ) {
    char a = char(charNum.get(i));
    fill(0, 192, 0);
    text(a, 60, y);
    y =20;
  }
}

void setup() {
  size(200, 300);
  background(209);
  charNum = new IntList();
  for (int i = 0; i < 12; i  ) {
    charNum.append(int(random(65, 65 24)));
  }
  display();
}

void draw() {
  frameRate(60); // Slow this to 1 to see changes
  charNum.set(index, int(random(65, 65 24)));
  display();
  index  = 1;
  if (index > charNum.size() - 1) {
    index = 0;
  }
}

  • Related