Bash Version of C64 Code Art: 10 PRINT CHR$(205.5+RND(1)); : GOTO 10

How about this?

# The characters you want to use
chars=( $'\xe2\x95\xb1' $'\xe2\x95\xb2' )
# Precompute the size of the array chars
nchars=${#chars[@]}
# clear screen
clear
# The loop that prints it:
while :; do
    printf -- "${chars[RANDOM%nchars]}"
done

As a one-liner with shorter variable names to make it more concise:

c=($'\xe2\x95\xb1' $'\xe2\x95\xb2'); n=${#c[@]}; clear; while :; do printf -- "${c[RANDOM%n]}"; done

You can get rid of the loop if you know in advance how many characters to print (here 80*24=1920)

c=($'\xe2\x95\xb1' $'\xe2\x95\xb2'); n=${#c[@]}; clear; printf "%s" "${c[RANDOM%n]"{1..1920}"}"

Or, if you want to include the characters directly instead of their code:

c=(╱‬ ╲); n=${#c[@]}; clear; while :; do printf "${c[RANDOM%n]}"; done

Finally, with the size of the array c precomputed and removing unnecessary spaces and quotes (and I can't get shorter than this):

c=(╱‬ ╲);clear;while :;do printf ${c[RANDOM%2]};done

Number of bytes used for this line:

$ wc -c <<< 'c=(╱‬ ╲);clear;while :;do printf ${c[RANDOM%2]};done'
59

Edit. A funny way using the command yes:

clear;yes 'c=(╱ ╲);printf ${c[RANDOM%2]}'|bash

It uses 50 bytes:

$ wc -c <<< "clear;yes 'c=(╱ ╲);printf \${c[RANDOM%2]}'|bash"
51

or 46 characters:

$ wc -m <<< "clear;yes 'c=(╱ ╲);printf \${c[RANDOM%2]}'|bash"
47

After looking at some UTF stuff:

2571 BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT
2572 BOX DRAWINGS LIGHT DIAGONAL UPPER LEFT TO LOWER RIGHT

(╱‬ and ╲) seem best.

f="╱╲";while :;do print -n ${f[(RANDOM % 2) + 1]};done

also works in zsh (thanks Clint on OFTC for giving me bits of that)


Here is my 39 character command line solution I just posted to @climagic:

grep -ao "[/\\]" /dev/urandom|tr -d \\n

In bash, you can remove the double quotes around the [/\] match expression and make it even shorter than the C64 solution, but I've included them for good measure and cross shell compatibility. If there was a 1 character option to grep to make grep trim newlines, then you could make this 27 characters.

I know this doesn't use the Unicode characters so maybe it doesn't count. It is possible to grep for the Unicode characters in /dev/urandom, but that will take a long time because that sequence comes up less often and if you pipe it the command pipeline will probably "stick" for quite a while before producing anything due to line buffering.

Tags:

Bash

C64