An hour of Pink Noise

cover made by netpbm, of course
an hour of soothing 2-channel noise

Direct download: 01-pink_noise.mp3

There are a million variations on the simple “use sox to play masking pink noise“, such as:

play -n synth pinknoise gain -3

This will play synthesized pink noise until you hit Ctrl-C.

But it you want two independent noise channels rather than mono, that’s a little more complex. It’s probably easier to download/play the MP3 file above than show you the command line.

Note that MP3s really aren’t designed to encode such random data, and it’s likely that your player will cause the audio to clip in a couple of places. I’m not quite sure why it does this, but it does it repeatably.

If you want to create this for yourself (and create a bonus lossless FLAC, which was far too large to upload here), here’s what I did to make this:

#!/bin/bash

duration='60:00'
fade='1'
outfile='pinknoise.wav'

# make the track
sox --combine merge "|sox --norm=-3 -c 1 -b 16 -r 44100 -n -p synth $duration pinknoise" "|sox --norm=-3 -c 1 -b 16 -r 44100 -n -p synth $duration pinknoise" -c 2 -b 16 -r 44100 $outfile fade $fade fade 0 $duration $fade gain -n -3

# make the cover
# 1 - text - 500 x 500 px
pnmcat -white -tb <(pbmmake -white 500 114) <(pbmtextps -font HelveticaBold -fontsize 64 -resolution 180 "PINK" | pnmcrop) <(pbmmake -white 32 32) <(pbmtextps -font HelveticaBold -fontsize 64 -resolution 180 "NOISE" | pnmcrop) <(pbmmake -white 500 114) > cover-text.pbm
# 2 - make the noise bg
pgmnoise 500 500 > cover-noise.pgm
# 3 - make the magenta text
ppmchange black magenta cover-text.pbm > cover-text-magenta.ppm
# 4 - overlay with transparency
pnmcomp -alpha=<(pnminvert cover-text.pbm | pbmtopgm 35 35 ) cover-text-magenta.ppm cover-noise.pgm | cjpeg -qual 50 -opt -baseline -dct float > cover.jpg
# delete the temporary image files, leaving cover.jpg
rm -f cover-text.pbm cover-noise.pgm cover-text-magenta.ppm

# make the mp3
lame -V 2 --noreplaygain -m s --tt 'Pink Noise' --ta 'Pink Noise' --tl 'Pink Noise' --ty $(date +%Y) --tc "scruss, 2021-05" --tn 1/1 --tg Ambient --ti cover.jpg "$outfile" 01-pink_noise.mp3

# make the flac (and delete wav file)
flac --best --output-name=01-pink_noise.flac --delete-input-file --picture=cover.jpg --tag="TITLE=Pink Noise" --tag="ARTIST=Pink Noise" --tag="ALBUM=Pink Noise" --tag="DATE=$(date +%Y)" --tag="COMMENT=scruss, 2021-05" --tag="GENRE=Ambient" --tag="TRACKNUMBER=1" --tag="TRACKTOTAL=1" "$outfile"

You’ll likely need these packages installed:

sudo apt install sox libsox-fmt-all ghostscript gsfonts-x11 netpbm lame flac libjpeg-progs

Applied Futility: Re-creating RAND’s ‘A Million Random Digits’

Sometimes, one must obey the inscrutable exhortations of one’s soul and travel deep into the inexplicable. The planet Why? has been left far behind, the chatter of its querulous denizens nothing more than a faint wisp of static. Where I’m going, pure patternlessness is all there is.

Page 53 from “A Million Random Digits …”, showing the five-digit sequential line number at left, followed by ten columns of five random digits each
A page from “A Million Random Digits …”, showing the five-digit sequential line number at left, followed by ten columns of five random digits each

In 1955, military-industrial complex stalwarts RAND Corporation published a huge book of just pages and pages of … digits. The digits were deliberately as random as possible, and were intended to help the fledgling practice of data science carry out truly random simulations. The book was called “A Million Random Digits with 100,000 Normal Deviates”. It was also made available on punched cards, with 50 digits to a card adding up to ten boxes of 2000 cards. A single box of punched cards was about 370 × 200 × 95 mm and weighed roughly 5 kg, so these digits had heft.

I thought it might be fun (or perhaps fun?: pronounced with a rising, questioning tone to stress the might-ness of any enjoyment arising) to dig into how RAND carried out this work, create some electronics to produce a similar random stream, and see how my random digits compare for randomness with RAND’s. For a final trick, I might even typeset the whole giant table into a book that no-one wants.

As I research and progress with this project, I’ll add in links here

I must stress: there is no reason for me to do this. A $5 micro-controller board can generate tens to hundreds of thousands of truly random digits per second. Any results I produce will have no use beyond my own amusement.


A Million Random Digits with 100,000 Normal Deviates is © Copyright 2001 RAND. Apart from a couple of page images and quotations from supporting material, none of that work is reproduced here. RAND does not support or endorse my futile efforts in any way.

Delicate tracings from a terrible PRNG

The two orbits of 16-bit RANDU, plotted as X-Y coordinates

I’d previously mentioned RANDU — IBM’s standard scientific PRNG in the 1970s that was somewhat lacking, to say the least —some time ago, but I found a new wrinkle.

For their 1130 small computer system, IBM published the Scientific Subroutine Package [PDF], which included a cut-down version of RANDU for this 16-bit machine. The code, on page 64 of the manual, doesn’t inspire confidence:

c     RANDU - from IBM Scientific Subroutine 
c     Package Programmer's Manual
c     1130-CM-02X - 5th ed, June 1970
c     http://media.ibm1130.org/1130-106-ocr.pdf

      SUBROUTINE RANDU(IX, IY, YFL)
      IY = IX * 899
      IF (IY) 5, 6, 6
 5    IY = IY + 32767 + 1
 6    YFL = IY
      YFL = YFL / 32727.
      RETURN
      END

(If you’re not hip to ye olde Fortran lingo, that bizarre-looking IF statement means IF (IY < 0) GO TO 5; IF (IY == 0) GO TO 6; IF (IY > 0) GO TO 6)

It accepts an odd number < 32768 as a seed, and always outputs an odd number. Yes, it basically multiplies by 899, and changes the sign if it rolls over. Umm …

There are two possible orbits for this PRNG, each 8192 entries long:

  1. Starting seed 1 (899, 21769, 7835, …, 18745, 9003, 1, 899)
  2. Starting seed 5 (4495, 10541, 6407, …, 28189, 12247, 5, 4495).

The plot above is the first series as X and the second as Y. I used INTEGER*2 types to simulate the 1130’s narrow integers.

∞ Noise

(Quick reminder, before it becomes obvious from the text — I have more interest in hardware random number generation than I have understanding …)

Just got Bill “WaywardGeek” Cox’s Infinite Noise USB Random Number Generator. It uses very few components, and doesn’t even have a microcontroller on board. It relies on the controlled amplification of thermal noise as its entropy source.

Not great enhanced image of the Infinite Noise board. Yes, that's all there is to it
Not great enhanced image of the Infinite Noise board. Yes, that’s all there is to it

As it’s so very simple, it uses a driver to read from the device, and then hashes the data to reduce the data stream to very close to pure noise. Building the driver is easy, once you work it that the code lives in the infnoise/software folder on the author’s github repo.

Normal operation would look like this:

sudo ./infnoise | entropy_consuming_program …

as in

sudo ./infnoise | rngtest -t 10

which I left running for a work day to get

…
rngtest: bits received from input: 10327720032
rngtest: FIPS 140-2 successes: 515955
rngtest: FIPS 140-2 failures: 431
rngtest: FIPS 140-2(2001-10-10) Monobit: 63
rngtest: FIPS 140-2(2001-10-10) Poker: 61
rngtest: FIPS 140-2(2001-10-10) Runs: 162
rngtest: FIPS 140-2(2001-10-10) Long run: 151
rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
rngtest: input channel speed: (min=29.022; avg=178.828; max=19531250.000)Kibits/s
rngtest: FIPS tests speed: (min=17.403; avg=30.153; max=85.917)Mibits/s
rngtest: Program run time: 56727702860 microseconds

So from its success to failure rate, it produces pretty decent (for my casual use) results. These bytes chug out at around 22¾ Kbytes/second; not screamingly fast, but decent, considering the very simple hardware.

You can run the hardware without hashing/whitening, and the results (from a much shorter run) are less solid:

sudo ./infnoise --raw | rngtest -t 10
…
rngtest: bits received from input: 15499264
rngtest: FIPS 140-2 successes: 0
rngtest: FIPS 140-2 failures: 774
rngtest: FIPS 140-2(2001-10-10) Monobit: 0
rngtest: FIPS 140-2(2001-10-10) Poker: 774
rngtest: FIPS 140-2(2001-10-10) Runs: 774
rngtest: FIPS 140-2(2001-10-10) Long run: 0
rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
rngtest: input channel speed: (min=27.201; avg=355.760; max=9765625.000)Kibits/s
rngtest: FIPS tests speed: (min=24.868; avg=30.488; max=41.554)Mibits/s
rngtest: Program run time: 49831593 microseconds

Another naïve test is seeing how images made from the data stream look:

don't stare at this too long, or richard d james's grinning mug will appear
random bytes (PNG), file size 49435 bytes

nvrandom20150121074051
raw bytes (PNG), file size 45421 bytes

Each of these 128 pixel squares should be no less than 49152 (= 128 × 128 × 3) bytes — plus the size of any PNG header/metadata — in size. The fact that the raw output is smaller shows that PNG’s compressor found some patterns it could work with.

It’s a fun little device, and Bill is adding new code and features to the driver at waywardgeek/infnoise regularly.

“Well, that was unexpected…”: The Raspberry Pi’s Hardware Random Number Generator

Hey! This is a bit old! Things may have changed and I haven’t necessarily fixed them.

Most computers can’t create true random numbers. They use a formula which makes a very long stream of pseudo-random numbers, but real randomness comes from thermal noise in analogue components. The Raspberry Pi has such a circuit in its SoC, as it helps making the seed data for secure transactions. It was only recently that a driver for this circuit was supplied. To enable it (on Raspbian): I think the module is enabled by default now for the different versions of the SoC.

  1. Make sure your system is up to date with
    sudo apt-get update
    sudo apt -y upgrade
  2. Install the module:
    sudo modprobe bcm2708-rng
  3. To make sure it’s always loaded, add the following line to /etc/modules (editing as root):
    bcm2708-rng
  4. For some RNG-related stuff, install rng-tools:
    sudo apt-get install rng-tools

The /dev/hwrng device should now be available, but can only be read by the root user.

Nico pointed out that you also need to:

  1. Edit /etc/default/rng-tools, and remove the # at the start of the line
    HRNGDEVICE=/dev/hwrng
  2. Restart rng-tools with
    sudo service rng-tools restart

What random looks like

random20130606210642random20130606210630

Random data look pretty dull. Here are random RGB values made with:

sudo cat /dev/hwrng  | rawtoppm -rgb 256 256 | pnmtopng > random$(date +%Y%m%d%H%M%S).png

(you’ll need to install the netpbm toolkit to do this.)

What random sounds like

Two short WAV samples of, well, noise:

Yup, sounds like static. It was made with the rndsound.sh script. You’ll need to install sox to run it.

This is not random

If it sounds like static, and even if it sometimes looks like static, it may not actually be true random noise. An infamous case of a pseudo random number generator being not very random at all was RANDU, which at first glance appeared to produce nearly random results, but close study showed it to be very predictable.

I wrote (what I think to be) a C implementation of RANDU: randu.c. While it produces appropriately random-sounding audio data (randu17.wav), if you output it as an image:

randu17_rgbThose stripes are a giveaway; there should be no order in the output. (Then again, I have no idea if I’ve implemented RANDU correctly.) Testing random data is hard, then — you really need a barrage of tests, and even some of them might fail even for truly random output. Thankfully, when you installed rngtools, it included rngtest, a simple checker for random data:

sudo cat /dev/hwrng | rngtest -c 1000
rngtest 2-unofficial-mt.14
Copyright (c) 2004 by Henrique de Moraes Holschuh
This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

rngtest: starting FIPS tests…
rngtest: bits received from input: 20000032
rngtest: FIPS 140-2 successes: 1000
rngtest: FIPS 140-2 failures: 0
rngtest: FIPS 140-2(2001-10-10) Monobit: 0
rngtest: FIPS 140-2(2001-10-10) Poker: 0
rngtest: FIPS 140-2(2001-10-10) Runs: 0
rngtest: FIPS 140-2(2001-10-10) Long run: 0
rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
rngtest: input channel speed: (min=67.969; avg=921.967; max=1953125.000)Kibits/s
rngtest: FIPS tests speed: (min=842.881; avg=3208.336; max=6407.890)Kibits/s
rngtest: Program run time: 27658884 microseconds

We were lucky that none of the tests failed for that run; sometimes there are a few failures. RANDU, on the other hand fares very badly:

./randu 17  | rngtest -c 1000
rngtest 2-unofficial-mt.14
Copyright (c) 2004 by Henrique de Moraes Holschuh
This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

rngtest: starting FIPS tests…
rngtest: bits received from input: 20000032
rngtest: FIPS 140-2 successes: 0
rngtest: FIPS 140-2 failures: 1000
rngtest: FIPS 140-2(2001-10-10) Monobit: 730
rngtest: FIPS 140-2(2001-10-10) Poker: 1000
rngtest: FIPS 140-2(2001-10-10) Runs: 289
rngtest: FIPS 140-2(2001-10-10) Long run: 0
rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
rngtest: input channel speed: (min=45.630; avg=14255.221; max=19073.486)Mibits/s
rngtest: FIPS tests speed: (min=23.694; avg=154.238; max=176.606)Mibits/s
rngtest: Program run time: 141071 microseconds

See? Lots of failures there. It’s hardly random at all. If you really want to get out testing randomness, there are the dieharder tests. They takes ages to run, though.

(Note: newish Intel machines also have a real hardware RNG in the shape of Rdrand.)

Bad Kids Jokes

Lost Tractor

what did the farmer say when he lost his tractor?

where’s my tractor?

Old Man

why is it when a old man with one kid people thinks ”stranger”, but when its a old man with 20 kids people think ”school trip” . im on to you old people

Mountain Climbing

Q) why did’nt the man clime up the mountain

A) because there wasn’t a mountain

— from Bad Kids Jokes (via)

how does he do that?

Someone asked how the automatic podcast works. It’s a bit complex, and they probably will be sorry they asked.

I have all my music saved as MP3s on a server running Firefly Media Server. It stores all its information about tracks in a SQLite database, so I can very easily grab a random selection of tracks.

Since I know the name of the track and the artist from the Firefly database, I have a selection of script lines that I can feed to flite, a very simple speech synthesizer. Each of these spoken lines is stored as as wav file, and then each candidate MP3 is converted to wav, and the whole mess is joined together using SoX. SoX also created the nifty (well, I think so) intro and outro sweeps.

The huge wav file of the whole show is converted to MP3 using LAME and uploaded to my webhost with scp. All of this process is done by one Perl script – it also creates the web page, the RSS feed, and even logs the tracks on Last.fm.

Couldn’t be simpler.

cosmic coincidence

Three consecutive space songs in today’s helping of the automatic podcast:

  • The Lovely Universe — Circulatory System
  • See The Constellation — They Might Be Giants
  • Kelly, Watch the Stars! — Air

That’s the thing about randomness – we see patterns that are of no import.

pgmrnoise – a more random (or less repeatable) pgmnoise

Update: If you have a recent NetPBM, this is fixed.
I’d previously alluded that netpbm’s pgmnoise wasn’t as random as it could be if you called it several times in quick succession. Nerdy discussion after the break, but here’s a (perhaps slightly linux-centric) alternative:

#!/bin/sh
# pgmrnoise - a more random pgmnoise; limited to 8-bit images
# created by scruss on Sun Oct 12 19:36:37 EDT 2008

echo P5
echo $1 $2
echo 255
dd if=/dev/urandom bs=$1 count=$2 2> /dev/null

I just pasted the shell text in there; you’ll need to save it as a file. It works the same way as pgmnoise:

 pgmrnoise width height > noise.pgm

It is limited as written to 8 bit-per-pixel output, but is a fairly trivial edit to make it 16 or more bits.

announcing the automatic podcast

Every day, the automatic podcast presents a random selection from my music collection. And I mean random: cartoon incidental music, snatches of folk songs, instrument tuition, and ancient scratchy 78s nestle up against my favourite indie hits. And it’s all introduced by a synthesized compère. I have no idea what’s going to be in it each day, and no records are kept of what was offered yesterday. It’s meant to be a daily snapshot, not an ongoing record.

There are still a few bugs to get out of the RSS feed, but generally I’m happy with how it works. There is some listener discretion required, as I can’t vet what goes into each day’s presentation.

Update, 30 Sep 2008: think I’ve fixed the RSS problems.

providing cannon fodder for empire since 1867

Prime Minister Stephen Harper says the toll of Canadian soldiers killed in Afghanistan is the price Canada is paying for playing a leadership role in world affairs.

I reckon that if I took a random street poll anywhere (anywhere outside Canada, that is), no more than 3 out of 10 people would consider Canada as having a leadership role. I do not wish to make light of the soldiers’ plight; I just don’t want them there in my name.

(I was going to make a comment about the nearest thing to a role to most Canadians would be a Swiss Chalet 1/4 chicken dinner, but that doesn’t work in a written context, and barely works when spoken.)

gone with the wind

I see that Americas Wind Energy updated their website to replace the site I wrote for them a couple of years back. It’s purty, but:

  • The page URL sometimes inexplicably switches to d3095932.ejt86.ejtechinternational.com from awe-wind.com.
  • The product page for the AWE 52-750 shows a bunch of non-operational turbines.
  • The AWE 52-900 page also has a picture of a parked turbine, and it looks a lot like Tallon Energy’s 52-750 at Pincher Creek.
  • More parked turbines on the 54-900 page, and occasionally a completely different machine is shown.

Oh wait, I get it – it’s a random turbine image for each page. Hmm.

completely not feeling the love for the iPod Shuffle

Shuffle mode on the iPod Shuffle isn’t random. It seems to play the same tracks in the same random order every time you restart the device. It only seems to get a new randomization when you sync with iTunes.

Oh yeah, and it’s too wide to fit alongside a standard USB plug on an iBook. I’ll check the BestBuy returns policy, ‘cos this thing just ain’t doing it for me.