The Art of Random Walks in MakeCode for Minecraft

A random walk is the path traveled by taking a step, then randomly turning either left or right, then repeating: a step, a random turn, a step, a random turn, and so on. Although a random walk sounds simple and is easy to code, it is an enormously important subject in many branches of science, such as physics, biology, economics and mathematics. Google Scholar lists 2,380,000 academic research papers on the subject.

1000 2 1

The science of random walks is not the subject of this blog. Rather, our aims are  to improve our coding skills in MakeCode for Minecraft and to explore random walk patterns as art.

Demo and How It Works

Program Inputs

  1. steps — the number of steps in the walk, maximum 3000, default 100.
  2. stepSize — the number of blocks in a single step, maximum of 10, default 3.
  3. blockGroup — is the group of blocks that can be used to create a random walk. The values can be 0, 1 or 2, default 0.
    • blockGroup 0 is LapisLazuliBlock or TNT depending or the random direction  (left or right).
    • blockGroup 1 choses a block randomly from a colorful array of choices.
    • blockGroup 2 choses a block randomly from a variations of black choices.

The Code

A simple random walk program called Agent Wanderer is found on Microsoft MakeCode for Minecraft site. The code below provides more flexibility by providing three arguments (see above) and using the Builder, which is faster than the Agent. the code below is shared at https://makecode.com/_AF0cdjKVu4zq, where it can be downloaded and run in MakeCode for Minecraft.

Random Walk Code

The code also includes a few utility Chats or function, which I plan to document in other posts. The utilities are:

  • Move Me — moves the player to the east and/or south the specified number of blocks.  Good for moving to a clean spot in the world.
  • Handy Compass– prints directions on the sky asynchronously. Handy for knowing the direction a random walk moves.
  • Go to Ground — moves the player to be “on the ground,” which is Y=4 in a flat world.
  • Canvas — replaces grass over a large area to create a white background for a random walk.

Further Explorations

  1. Add code to determine the percentage of the time does a random walk wander to the north, to the south, and so on?
  2. Add a counter for the number of  right turns as opposed to left turns.
  3. Add a counter for the number of blocks that are south of the origin, east of the origin, and so on.
  4. How often does a walk return to land on  the origin block during its path?
  5. Instead of replacing blocks that have already been replaced, stack them; thus, counting the number of times each block has been traversed.

A particularly intriguing activity is to try replacing

run chat command dir

with

 

call function dirFunction
What happens? Why?  This will be covered soon in another blog.