10 Print on Windows Command Line

Posted on Friday, 29 December 2017

What is 10 Print?

For those who don’t know about 10 Print, it is the name of a one line command that you can type on a Commodore 64. The command is

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

That command, if executed in the Commodore 64, will produce this image:

10 print original

As you can see, the command basically tell the computer to print out the / (forward slash) or \ (backward slash) character randomly.

Adapting 10 Print to Windows Command Prompt

There are two ways to execute the 10 Print command on Windows cmd, either using a batch script or by directly executing on the command line. The advantage of using the batch script is that the commands can be more easily understood, while the advantage of executing the command directly on cmd is that you can type it in a single line and that is probably cooler.

10 Print on Batch Script

Below is the 10 Print script.

@echo off
set max_number=32767
set /a threshold=%max_number%/2
:loop
if %random% lss %threshold% (echo|set /p=/) else (echo|set /p=\)
goto loop

Copy the text above to a text editor (e.g. Notepad), and save it as 10print.bat . Next, you should open the command prompt and change the directory to the location of 10print.bat. Before executing the script, you should change the font for maximum effect. To do this, you should right-click on the top bar of cmd (or type Alt+Space) and click Properties. Change the font to Raster Fonts and set the size to 8 x 8. The goal is to make every character’s height and width the same size.

Next, you can just execute the script by tying 10print.bat on the cmd and type enter. The result should be like the picture below:

10print-batch

Cool, isn’t it? Although it does not really look like a maze. To stop the program you only need to type Ctrl+C.

Script Explanation

Now I will go on to explain each line in the script.

  1. @echo off: if we don’t do this, each command in the script will be echoed. I encourage you to try to delete this line (or comment it out by adding :: in the beginning of the line) and try to execute the script again.
  2. set max_number=32767: this is how you create a variable (in this case the variable is called max_number) in a batch file. This variable will later be used alongside the random number, because 32767 is the largest number that can be generated by the random number generator. (Note that the number 32767 is not arbitrary, you are encouraged to find out the meaning of that number.)
  3. set /a threshold=%max_number%/2: this will also create a variable, which is equal to max_number divided by 2. We need to use the /a after the set because we are setting a variable to a mathematical operation. The threshold will be the number that determines whether the computer should print out a / or \.
  4. :loop: create a label named loop.
  5. if %random% lss %threshold% (echo|set /p=/) else (echo|set /p=\): this is the most important part of the script. Let’s break this line further:
    1. %random% is a special variable in batch file that will return a random number, which ranges from 0 to 32767.
    2. lss is “less than” comparator. E.g. to evaluate 2 < 3 you will type 2 lss 3 in the batch file.
    3. %threshold% this is to use the variable threshold that we have created before.
    4. if %random% lss %threshold%: so, this line is evaluating whether the random number generated is less than the threshold or not. If it is, then the command (echo|set /p=/) will be executed, otherwise the command (echo|set /p=\) will be executed.
    5. echo|set /p=/: print out a forward slash without a newline. Why can’t we just use echo /? Because the cmd will print out a newline after printing the /. So, we need to put |set /p= in between echo and /.
    6. echo|set /p=\: print out a backward slash without a newline.
    7. goto loop: this command will tell the cmd to start the executing commands from the label loop that we have created. Essentially this will create an infinite loop.

10 Print on Command Line

Now we are going to create the same effect but only on one line. These are the steps:

  1. Open the cmd. Change the font like before.

  2. Type in cmd /v and press enter (explained later).

  3. Type echo off and press enter.

  4. Type this command and press enter:

    for /l %n in () do (if !random! lss 16383 (echo|set /p=/) else (echo|set /p=\))
    
  5. Finished! To stop the program press Ctrl+C. To make the cmd appear like before, just type in echo on and press enter.

Explanation

Firstly, why do we need to type in cmd /v first? We do that to create a new instance of cmd with “delayed local expansion.” Without doing this, the random number generated will be the same at every iteration! Try this yourself. Try to follow the steps above but ignoring the second step. The result will either be //// or \\\\\.

The reason why we need to type in echo off is the same as before.

Now, what is the meaning of this:

for /l %n in () do (if !random! lss 16383 (echo|set /p=/) else (echo|set /p=))
  1. for /l %n in (): this will create an infinite loop.
  2. !random!: create a random number that is different at each iteration. If we just use %random% the cmd will always give the same number at each iteration.
  3. The number 16383 is equal to the variable threshold that we used before.