Hacker News new | ask | show | jobs
by anyfoo 236 days ago
Maybe you'll get a kick out of this, I did it in a bored afternoon a while back:

    echo -ne '\e[8;32;90;t';n=20;t=524292;l=$((t-1));m=$((2**n-1));c=0;xs=(1);ys=(1);for ((i=0;i<n*m;i++));do b=$((l&1));l=$(((l>>1)^(b*t)));c=$(((c<<1|b)&m));[ $((i%n)) -eq 0 ] &&{xs=($c $xs[1,4]);y=$((((xs[4]-xs[0])<<30-0x3fffd60f*ys[2]+0x7d32617c*ys[1])>>30));ys=($y $ys[1]);yd=$((120+(y >> 24)));printf '\e[48;5;%um ' $yd};done
It is entirely self-contained (but needs zsh, not bash, for dumb reasons). Terminal at 90 columns works best.

It is just a very simple integer LFSR as a random number source, followed by a hand-made integer IIR filter (manually placing poles on the z-plane). All of this entirely with trivial integer operations only (effectively using 32 bit fixed point arithmetic)

So without any external input or tools at all, and not even using zsh's $RANDOM, it makes an "analog" weavy pattern.

The LFSR is this part:

    b=$((l&1));l=$(((l>>1)^(b*t)));c=$(((c<<1|b)&m))
The hand-made filter is this part:

    xs=($c $xs[1,4]);y=$((((xs[4]-xs[0])<<30-0x3fffd60f*ys[2]+0x7d32617c*ys[1])>>30));ys=($y $ys[1])
I specifically tuned the filter peak just slightly away from being an integer divisor of 90 columns, to give the pattern a slight "rolling" effect.*
1 comments

What... the... fuck. Mind blown.

The zsh dependency is super unfortunate, though :(

It is amazing! With some brutal hacks to some array indices, and probably some excessive quoting changes it can work in Bash too:

  echo -ne '\e[8;32;90;t'; n=20; t=524292; l=$((t-1)); m=$((2\*n-1)); c=0; xs=(1); ys=(1); for ((i=0; i<n*m; i++)); do b=$((l&1)); l=$(((l>>1)^(b*t))); c=$((((c<<1)|b)&m)); if ((i%n==0)); then xs=("$c" "${xs[@]:0:4}"); y=$(( ((xs[4]-xs[0])<<30) - 0x3fffd60f*ys[1] + 0x7d32617c*ys[0] )); y=$((y>>30)); ys=("$y" "${ys[@]:0:1}"); yd=$((120+(y>>24))); printf '\e[48;5;%um ' "$yd"; fi; done
This could probably be submitted to https://github.com/attogram/bash-screensavers/blob/main/gall...