Wanting advise on how I could randomize my work more


#1

I am working on a project where I use a keylogger to collect button input, let Processing read it and let it do certain actions when it goes over the specific letter. I need to make the program somewhat random, and I am tryin to get that effect if it comes to the directions of the drawing.

Posted someting about it before here and even got a comment to implement some sort of Shuffle-process, but it is yet something I have to investigate (mainly due lack of knowledge and a time crunch)

Anyway. Currently my code has somewhat specific values. The speed is slightly random, and so is the position. The issue is: The line always moves to the lower-right corner, and restarting the program (resetting its position on a random place from 0 to width and 0 to height) always results in a somewhat same-ish pattern. I want to try to break free from it.

TekenLine drawing;
String[] buttonLog;
char[] letters;
int charIndex;
//boolean isA = false;

void setup() {
  size(600, 800);
  background(255);
  buttonLog = loadStrings("buttonLog.txt");
  letters = buttonLog[0].toCharArray();
  drawing = new TekenLine(int(random(0, width)), int(random(0, height)), int(random(1,3)),int(random(1,3)));
}

void draw() {
  switch(letters[charIndex]) {
  case 'a': 
    fill(#CC00CC);
    //isA = true;
    break;
  case 'w': 
    fill(#404040);
    //isA = false;
    break;
  case 's': 
    fill(#0000CC);
    //isA = false;
    break;
  case 'd': 
    fill(#00CCCC);
    //isA = false;
    break;
  }
  charIndex = (charIndex + 1) % letters.length;
  drawing.drawLine();
  drawing.moveLine();
  drawing.bounceLine();
}

Class :

class TekenLine {

  float x;
  float y;
  float lineWidth;
  float lineHeight;
  float xSnelheid;
  float ySnelheid;


  TekenLine(int tempX, int tempY, int tempXSnelheid, int tempYSnelheid) {
    
    //x = width / 2;
    //y = height / 2;
    
    x = tempX;
    y = tempY;
    xSnelheid = tempXSnelheid;
    ySnelheid = tempYSnelheid;
    lineWidth = 10;
    lineHeight = 10;
    //xSnelheid = 5;
    //ySnelheid = 5;
    
  }

  void drawLine() {
    noStroke();
    rect(x, y, lineWidth, lineHeight, 20);
  }

  void moveLine() {
    x += xSnelheid;
    y += ySnelheid;
  }

  void bounceLine() {
    //if(isA){
    // xSnelheid = -xSnelheid;
     
    //}
    
    if (x >= width || x <= 0) {
      xSnelheid = -xSnelheid;
      //lineWidth = (random(10, 20));
    }
    if (y >= height || y <= 0) {
      ySnelheid = -ySnelheid;
      ySnelheid = ySnelheid*random(0.8,1.2);
      //lineHeight = (random(10, 30));
    }
  }
}

Currently, as mentioned before, my code goes into the same direction, but each from a different height and width each restart, bouncing from the walls and changing colors assigned to “W. A. S. D” from the buttonlog.text log.

I have a certain ideas, but do not know how to start or implement it. The ideas are:

  • if letter = A (for instance), then slightly turn to the left/right
  • if letter = W, then move to random direction.

Probably not the best examples that I can think of, but it is something that I need to make the movement of my drawing-line a bit more randomized so that each new re-start can be a little bit more unique.

Any ideas are welcome!


#2

Just use random (-4,4) in last line of setup ()


#3

I used to do that before, issue is, it still seems to stick to a “regular” pattern. I’m still trying to find out how it can randomize itself too when the program is running. as I said in my post, for example:

  • if letter = A (for instance), then slightly turn to the left/right
  • if letter = W, then move to random direction.

#4

Does it only log wasd letters or all of them?


#5

Can you please post as text a typical text produced by the keylogger?

In theory the switch () command has to change xsnelheid and ysnelneid in the class

Either by adding something or subtract something or by setting both to a new value with random (-4,4);


#6

To answer both your questions:

The KeyLogger is a form of code that runs in Powershell. I noticed it only logs A to Z, 1 to 0 and all the other characters except for CTRL, ALT, SHIFT, ESC etc. etc.

A line of text from the .txt file could be: aaawwwwwwaasssdaawwsddsssssddaaaaaa for instance. All on 1 line.


#7

but a text could also be NMBKUWZZOzoidzhfkvbmnbmsb, right?


#8

Regards, Chrisir :wink:

// The letter is converted to ascii (by int) and then used as an angle for a circle. 

// The angle is used to set the direction (xSnelheid and ySnelheid) in 
// the class TekenLine.
// The min and max value of all found asciis are used and the circle is calculated 
// in a way that min ascii is 0 degree and max ascii is 360 degree. 

// To change this circle more, offset is used that slowly rotates the circle. 
// We could also shuffle letters.
// Obviously the randomness is higher, when we have all kind of letters and not 
// only wasd. It's also higher the longer the given keylogger text is (since the
// text is used again and again). 

// https://discourse.processing.org/t/wanting-advise-on-how-i-could-randomize-my-work-more/9267/5

TekenLine drawing;

char[] letters;
int charIndex;
int minAngle=19999, maxAngle=-1000; 
float offset=0;

void setup() {
  size(600, 800);
  background(255);

  String[] buttonLog;
  buttonLog = new String[1];
  // buttonLog = loadStrings("buttonLog.txt");
  buttonLog[0] = "NMBKUWZZOzoaaawwwwwwaasssdaawwsddsssssddaaaaaaidzhfkv bm01234556789nbmsb";
  // buttonLog[0] = "aaawwwwwwaasssdaawwsddsssssddaaaaaa";

  letters = buttonLog[0].toCharArray();

  // identify min and max value
  for (char c1 : letters) {
    int intValue = int (c1); 
    if (intValue<minAngle) {
      minAngle=intValue;
    }
    if (intValue>maxAngle) {
      maxAngle=intValue;
    }
  }//for

  // make object from class
  drawing = new TekenLine(int(random(20, width-33)), int(random(20, height-33)), 
    int(random(-3, 3)), int(random(-3, 3)));
}//setup

void draw() {

  int intValue = int (letters[charIndex] ); 
  // println (angle);
  float angle = map(intValue, 
    minAngle-10, maxAngle+10, 
    0, 360); 

  float r = 11.2; // speed  
  float x =  r * cos(radians(angle+offset)) ;
  float y =  r * sin(radians(angle+offset));

  drawing.setSnelheid(x, y); 
  drawing.bounceLine();

  fill(angle, 0, 0); 

  switch(letters[charIndex]) {
  case 'a': 
    fill(#CC00CC);
    //isA = true;
    break;
  case 'w': 
    fill(#404040);
    //isA = false;
    break;
  case 's': 
    fill(#0000CC);
    //isA = false;
    break;
  case 'd': 
    fill(#00CCCC);
    //isA = false;
    break;
  }

  charIndex = (charIndex + 1) % letters.length;
  offset+=random(.4, 1.27);

  drawing.drawLine();
  drawing.moveLine();
  drawing.bounceLine();
}//draw

// ==============================================================

class TekenLine {

  // class mover 

  float x;
  float y;

  float lineWidth;
  float lineHeight;

  float xSnelheid;
  float ySnelheid;


  TekenLine(int tempX, int tempY, 
    int tempXSnelheid, int tempYSnelheid) {

    //x = width / 2;
    //y = height / 2;

    x = tempX;
    y = tempY;
    xSnelheid = tempXSnelheid;
    ySnelheid = tempYSnelheid;
    lineWidth = 10;
    lineHeight = 10;
    //xSnelheid = 5;
    //ySnelheid = 5;
  }

  void drawLine() {
    noStroke();
    rect(x, y, lineWidth, lineHeight, 20);
  }

  void moveLine() {
    x += xSnelheid;
    y += ySnelheid;
  }

  void bounceLine() {

    if (x >= width-5 ) {
      xSnelheid = -abs(xSnelheid);
      //lineWidth = (random(10, 20));
    } else if ( x <= 0) {
      xSnelheid = abs(xSnelheid);
    }

    if (y >= height) {
      ySnelheid = -abs(ySnelheid);
      ySnelheid = ySnelheid*random(0.8, 1.2);
      //lineHeight = (random(10, 30));
    } else if ( y < 5) {
      ySnelheid = abs(ySnelheid);
      ySnelheid = ySnelheid*random(0.8, 1.2);
    } // else if
  }// method

  void setSnelheid(float x_, float y_) {

    // set direction (but keep pos or neg)

    // monitor if the values are neg
    boolean xIsNeg=false; 
    boolean yIsNeg=false;

    // monitor if the values are neg
    if (xSnelheid<0)
      xIsNeg=true;
    if (ySnelheid<0)
      yIsNeg=true;

    // set value 
    xSnelheid=x_;
    ySnelheid=y_;

    // apply if negative
    if (xIsNeg) 
      xSnelheid*=-1;
    if (yIsNeg) 
      ySnelheid*=-1;
  }
  //
}
//

#9

Holy moly that is a lot to process lol.

Thank you, do you mind explaining tho what moat of your code means? (Mainly because I am still very beginner)


#10

It’s all explained at the beginning of the code

The letters are interpreted as angles / degrees in a circle

The line goes where the angle points to


#11

Hey there, so I gave your code a full-look, and I have a few questions left if you do not mind answering them!

  1. First of all, I have a hard time figuring out what you mean with the following code
int minAngle=19999, maxAngle=-1000; 

for (char c1 : letters) {
    int intValue = int (c1); 
    if (intValue<minAngle) {
      minAngle=intValue;
    }
    if (intValue>maxAngle) {
      maxAngle=intValue;
    }
  }

What exactly does the value forint minAngle=19999, maxAngle=-1000; represent, and what exactly does for (char c1 : letters) do?

  1. I noticed that you left the tempXSnelheid and tempYsnelheid in the
drawing = new TekenLine(int(random(20, width-33)), int(random(20, height-33)), 
    int(random(-3, 3)), int(random(-3, 3)));

line (the last 2 random INTs). I noticed you already determined the snelheid somewhere else, being at: void setSnelheid(float x_, float y_). Could I remove the INT(random) parts in my TekenLine and the xSnelheid and ySnelheid floats, because you are setting up the speedvalue there anway, or is it for something else?

  1. in regards to float angle = map(intValue, minAngle-10, maxAngle+10, 0, 360);
    Could you tell me exactly what is going on in this part?

  2. I see you are doing a check for a negative value under the void setSnelheid tab. Could you also explain me why this would be needed?

Sorry if you have to write up a lot, it is just that I cannot wrap my head around it myself.


#12

We are searching the min and the maximum letter within letters. The for loop is a short version of saying in each for loop iteration give c1 one char in letters, the 0th, then the 1st etc.

Both min and maximum variables get an initial value that makes sure the if clause condition is met once - being very small or very big.

I think you can leave the snelheid out in the constructor of the class since we set them later throughout

We translate the ascii value of the letters to an angle in degrees- see reference for map()

When we modify the snelheid we change the angle of the movement.

But (this is probably important for a situation when we are close to the screen border) we keep the +/- sign so the general direction stays the same when we apply a new letter.

I am not sure why I did this, probably I experienced unwanted behavior of the line progress. For example it could occur when we just bounced on the right wall we are going left now. When now the letter commands to go right we would be trapped on the wall. To avoid this we keep the general direction left!