Counting Random 0s and 1s in MakeCode for Minecraft

Real-world games like Monopoly and Yahtzee use dice rolls to pick random numbers. Arbitrary decision like who goes first or which end of the field a team starts are decided by the flip of coin, which can be thought of as a random number either 0 or 1.

A coin flip has a 50% chance of landing on heads and a 50% chance of landing on tails. This is called a “50/50 chance” or “50-50 chance,” which means the same thing. Heads or tails is a “binary” choice because  there are only two possible outcomes. Other binary choices include  yes or no, true or false and 0 or 1.

Computer games, including games written in MakeCode for Minecraft, also use random numbers to make the games interesting and  unpredictable. For example, the game Grandma’s Chickens uses random numbers to decide where and when lightening will strike killing chickens and burning holes in the fence so that the chickens can escape.

If you flip a coin 20 times, will it land on heads 10 times and tails 10 times? You might think so because the chances are 50/50. But does it?

Let’s Experiment Using MakeCode for Minecraft

We’ll explore this by using MakeCode for Minecraft  to pick a random numbers that are either 0 or 1, where. 1 will represent heads, 0 tails. The MakeCode block that picks a random number that is either 0 or 1 is

random 0 to 1

By picking 20 such numbers and counting 1’s and 0’s separately,  we might be able to answer our question. The result of our code will be similar to that shown below. The yellow blocks are the count of 0’s, the blue blocks 1’s. The output from three runs of the program are shown.

50 50

Only one of these three results picked 10 0’s and 10 1’s. In the lingo of heads and tails, one time we tossed a coin 20 times, the result was 13 tails and 7 heads, another was 11 tails and 9 heads and the last was 10 tails and 10 heads. Why? Two reasons:

  1. 20 random numbers is a ridiculously small sample. I bet you have experienced this in games when it seems like dice throws are anything but random.
  2. These are computer-generated random numbers, which are called “pseudorandom numbers” because they are not perfectly random.

Let’s see the code in action.

The Code

The MakeCode for Minecraft file is available at this URL, which can be pasted into a browser or imported in MakeCode for Minecraft. The code below is explained in the above video.

Count 1 and 0 Code