Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: No Current Assignments

Computer Programming I :: Lessons :: Minecraft Random Numbers

Random Numbers

Random numbers can make a Minecraft program more interesting by adding a degree of unpredictability. To do something random in Minecraft, you first have to create a random number using the Random Number command, which creates a random number between 1 and the number you place after it.

Minecraft Random Number Command

The above example will create a random number between 1 and 5 and store it in the variable called slot.

Counting Items in a Slot

You can use a variable instead of if statements to pick a slot. We can combine this with random numbers to randomly select a slot.

Minecraft Select Slot with a Variable

In the tower program, we used a variable to keep track of how many blocks are left. Any easier way is to use the Item Count command. The command will tell you how many items are left in the current slot.

Minecraft Item Count Command

Else If Statements

An else if statement is used in between an if statement after an if statement but before an else statement. You can have as many else if statements as you want connected to an if statement, but only one of them will run (or the original if statement will run). The else if statements are checked in the order they appear, and if none of them are true the else statement (if it exists) will run.

Minecraft Else If Statements

Random Tower Program

We previously built a tower that alternated slots every other floor. Let's instead build a tower that randomly selects from slots and continues building until one of the slots is empty. To add another twist, let's have it skip a floor for certain slots. Below is the algorithm for our random tower:

  1. Select a random slot.
  2. While the current slot has at least 16 blocks, repeat steps 3-9.
  3. Move the turtle up one block.
  4. If the current slot is one or two, move the turtle up one.
  5. Put a block below the turtle and move forward.
  6. Repeat step 5 a total of 4 times.
  7. Turn right.
  8. Repeat steps 5-7 a total of 4 times.
  9. Select a random slot.

The following image represents the entire tower program, which we will examine step by step.

Minecraft Random Tower Program

Step 1: Select a random slot.

Minecraft Random Tower Program We pick a random number between 1 and 5 because there are five filled slots in this turtle. The number is stored in the variable called slot. We then select that slot using the slot variable.

Step 2: While the current slot has at least 16 blocks, repeat steps 3-9.

Minecraft Random Tower Program We will use a while loop for this step to make sure the current slot has at least 16 blocks. That is how many blocks we need to build a single floor of a 4x4 tower. We use the item count command to check the number of items in the current slot.

Step 3: Move the turtle up one block.

Minecraft Move Up Command This step is pretty simple. Just use the move up command to make the turtle move up a single block.

Step 4: If the current slot is one or two, move the turtle up one.

Minecraft Random Tower Program

Step 5: Put a block below the turtle and move forward.

Step 6: Repeat step 5 a total of 4 times.

Step 7: Turn right.

Step 8: Repeat steps 5-7 a total of 4 times.

We need a variable to determine if the turtle should move up. We'll call that variable skip and set it equal to 1 if the slot is equal to 1 or the slot is equal to 2. Otherwise, we set skip to 0. In our repeat loops, we check to see if skip is 1. If it is, we set it to 0 and move the turtle up. The rest of this code works the same as our original tower program.

Step 9: Select a random slot.

This is the same code that we had in step 1 except it is after our repeat loops.

After running the program, you will have a random tower.

Minecraft Tower
Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram