Introduction: How to Make a "How To" Generator Using Arrays in Processing

The purpose of this Instructables is to show you, step-by-step, how to make a random phrase generator using arrays in the free program, Processing.

This is a 12-step program, so it will not take that long!

I attached a copy of my code, if you need dire help working on your code.

-Ivanrey Barlongo

Southwest Career Technical Academy

Step 1: Open Processing and Begin With the Basic Functions.

Launch the application, and make sure you have a safe place to save your file too.

After Processing is opened, you should have a blank empty sketch.

The basic functions we will need to complete our code is void setup() and void draw().

The purpose of void setup() is to run our processes once, for example, generate a "How to phrase"

The purpose of void draw() in out code is to make our "How to phrase has some place to appear other than the console area. Note: The console area is the black bar at the bottom of your blank sketch in Processing.

Step 2: Making Our First Variable

Note: These variables will be located above void setup(), so make room for them.

This first variable will contain our array of information we will want our "How to" generator to say,

such as "How to eat dry ice?"

The variable will will use to to show the array of our information is String phrase [ ] = {

Note: the name "phrase" after string is just a quick name we want our string to be identified by. In addition, note that we are using brackets ([ ]) in this string because it shows we are making a list.

After the curly cue in String phrase [ ] = {

This is where you will write the information for your array, such as
String phrase[] = { "Earn Respect",

Continue to keep adding information you would want your "How to" generator to say in the end.

Note: the quotations around our information, which is crucial to have.

The end result you should have is 25 phrases in this string.

Step 3: Making Your Final 2 Variables

The final 2 variables we will need in our code deals with the font we will want on our screen when it prints the generator, and a way to randomize our generator my position.

In order to make our fonts for later in the code we will need the variable: PFont f;

Note: Do not forget to add the semicolon at the end

The next variable we will need to randomize our information in "String phrase [ ] = {" by position is written as:

int phr = int(random(phrase.length));

Note: This is a variable that will randomize the integer phr, shortened version for phrase, to return the positions of the information in our array.

Now we have a randomizer! Now we just need to find a way to transfer the random numbers into words.

Step 4: Starting Your Void Setup()

First of all, we will need to format our screen if we were to press run

We can do this by setting a size, and picking a color.

For example:

void setup(){
size(400,200);

fill(#FFFFFF);

size(); will re size the screen to 400px by 400px

fill(); will change the color of the screen, and currently #FFFFFF makes the screen white.

Finally, we can add the type of font/size we will be using in our screen.

We will use the function: f = createFont("Harrington",16,true);

This function will allow us to make the font: Harrington, and make sure it proves true.

Step 5: Creating Your Functions

Now this is what our final product of our functions will look like, so do not worry we will explain how to make the functions next step.

phraseOne(); is a function that was named this because it gives a quick explaination of what it will do.

DO NOT WORRY ABOUT ADDING ALL THESE FUNCTIONS IN THE PICTURE BELOW VOID SETUP() RIGHT NOW BECAUSE WE WILL DO THAT IN A LATER STEP.

Step 6: How Functions Work and How to Create Them

First of all, the purpose of a function is to perform an action, and we will be putting these functions below void setup() because we will only want them to run once.

In order to create a function, we will have to start void then the name we want to call our function, and add curly braces.

For example: void phraseOne(){

The goal of this function will be to print "How to earn respect?" if the random position number generator is equal to 0.

To begin our function, we will start with an if statement because it will help us to make the random number generator to be able to correspond to print a How to. Then will want it to print "How to earn respect?" if it returns true.

It will look like this:

void phraseOne(){

if (phr == 0) { //if the random number position generator is equal to 0

println("How to "+"Earn Respect?"); // then print "How to Earn Respect?" in the console area

}; //This will close the curly cue from the if statement

} //This will end the function and void phraseOne() is done

So then now, since we are done with this function, we will be able to add it to void setup(){

because we will only want it to run once.

It will appear as phraseOne();

below void setup, as in the picture.

Step 7: Keep Repeating Making the Functions

After you were able to complete phraseOne(); function, now repeat the same process until you have successfully finalized all your How to phrases. This process can be repeated up to 25 times if you wish, as I did in my code.

As the end result, after your void setup, your functions should appear such as:

phraseOne();

phraseTwo();

phraseThree();

~Which activates the function we made to run once in void setup, as I did in the picture.

Congragulations! You now have successfully made a "How to" Generator for the console area.

The console area is the black bar in the bottom of the screen, and it will print a random How to Phrase if you press run.

However, you can stop here if this is all you wanted, but we can take it a step further by printing it in a window rather than the console area.

Step 8: Congratulations You Made a "How To" Generator Now for the Console Area!

If you followed the previous steps correctly, you should now have a "How to" Generator working for the console area!

However, we can take this a step further by making this text print on a screen when we press run.

Step 9: Now That It Prints in the Console Area, Lets Print It on the Screen!

First of all, finally close void setup() because we are done with it for now.

You can close this by just adding a left curly cue ( } ) at the end of void setup()

Now we will start void draw(){

The first thing we will want in our void draw(){ is the color of the background, and the type of font we will want to use in our generator.

We can do this by adding:

void draw(){

background(255); //The color of the screen, which will be white

textFont(f,16); //The type of font (f), which was Harrington according to our void setup, and 16 will be the size

Step 10: Finally, We Will Need to Add the Final Functions Below Void Draw(){

We will make the text print from the console area to the screen by using an if statement for each phrase.

For example, we will want the screen to print the text if the random number position generator is true to the phrase.

We will do this by:

if (phr == 0){ //If the random number position generator is 0,
text("How to "+"Earn Respect?",10,100); }; //then print this phrase in the screen

// Note: 10,100 is the x and y position it will print on the screen

If we add this conditional statement below all of our variables in void draw(){ It will work properly.

So, in theory, if our random number position generator, which was int phr = int(random(phrase.length)),

randomly gives the number 0, the screen will print "How to Earn Respect?" on the screen.

Step 11: Keep Repeating on Making the If Statements!

Don't worry we are almost done!

Finally, we will need to keep making these if statements for each phrase we had in our array

For example,

if (phr == 0){
text("How to "+"Earn Respect?",10,100); };

if (phr == 1){ //Continue to make an if statement for each phrase you have!

text("How to "+"Get Ahead in Life?",10,100);

if (phr == 3){
text("How to "+"Make Spinach Pie?",10,100); };

Now this may be a tedious process, but the end result is worth it. In my code, I had to make 25 if statements because I had 25 phrases in my array, which was in String phrase[ ]

Step 12: You Are Done With Your Code!

After you add all your if statements, all you need to do is close your void draw(){

Also make sure everything is closed, and functioning correctly in your code!

You are finally done with your code, and now yous should have a functioning "How to" Generator that prints on the screen!

You can make any edits to the code if you would like to make a random word/phrase generator.

Anything is possible!

Happy Coding!