Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The challenge is: generate an audio file made of white noise.
Requirements and instructions:

  • Your program must generate an audio file containing only white noise, meaning its intensity is the same for all (reasonable) frequencies and nonzero [see an example plot];
  • One must be able to play the audio file on the latest version of VLC [at the time of writing your answer];
  • The program doesn't need cryptographically-strong randomness, rand functions or reading from /dev/rand is okay;
  • The program must be able to generate at least 1 hour of audio, at least in theory (meaning system limitations like maximum filesize don't apply);
  • Your score is the number of bytes in your source code, plus all bonuses that apply;
  • Using any third-party, external library is okay;
  • The program must work without access to the Internet.

Bonuses are:

  • -15%: Allow to specify the format of the audio file (at least two choices; the number of possible choices doesn't change the score);
  • -10%: Allow to specify the duration of the audio file;
  • -5%: Allow to specify the bitrate of the audio file.

The settings can be stored in variables, files, or given as command line parameters, your choice. Percentages are calculated from the original number of bytes, before any bonus is applied.

share|improve this question
The problem spec seems to be describing a code colfing challenge, but the tags say otherwise. – undergroundmonorail Mar 5 '14 at 16:02
Whoops! I'll fix that. – Giulio Muscarello Mar 5 '14 at 16:03
Perhaps the challenge should be revised to only allow "existing third party libraries" instead of "any library". – jpmc26 Mar 6 '14 at 1:02
Are you going to be a stickler on the noise being white? If you really want to verify: its autocorrelogram should have a clear spike around zero, and almost nothing at any other delay. If you aren't going to be a stickler, I say that it is pointless to specify that it be white noise. Just say noise. – Tim Seguine Mar 6 '14 at 15:13
up vote 3 down vote accepted



writes a new WAV file to disk called a. It has a sample rate of 8 kHz and a 16 bits per sample in signed integer format. The source data is uniformly distributed on the interval [0,1], which is mapped to the interval [0,32767] after the conversion to integer format.

MATLAB, 28 - 4 (10% + 5%) = 24

I'm not sure what the OP meant about how settings could be stored in variables, but I interpreted it in a way that is favorable to this case. Assuming that:

  • The desired bit rate (in bits/second) is provided by the user in the variable b. The bits per sample is hard-coded at 16.

  • The desired duration of the file (in samples) is given in the variable d.

The result is:


MATLAB, 16 - 4 (15% + 10%) = 12

Adding another layer of sleaze in pursuit of bonuses, I make another assumption: the desired function to use to output the file should be specified in the variable f. Then the code simplifies to:


Allowable values for the function are:

f = @wavwrite


f = @auwrite

Each function will cause the above snippet to write out a file of the appropriate format (WAV or .au) at a sample rate of 8 kHz with the specified duration. I took off the bonus for specification of the bitrate here, because auwrite defaults to 8 bits per sample instead of 16 like wavwrite does. I don't see a way to harmonize the two without using more characters.

share|improve this answer

Bash, 34

dd if=/dev/sda of=file.wav count=1

If you don't want hard drive 'randomness', (a lot slower)

dd if=/dev/random of=file.wav count=9

SPIN, 28

word x=0
share|improve this answer
Somehow I don't think the file header can be entirely random / the same as the hard disk drive first sector – Jan Dvorak Mar 5 '14 at 16:10
Herr Doctor may be right, if the test harness is VLC. VLC is pretty forgiving. – Jonathan Van Matre Mar 5 '14 at 16:12
You've generated noise, but is it white noise? – Steve Mar 5 '14 at 16:20
/dev/random also exists on Macs, yay! Unfortunately iTunes can't play the resulting wav file. Not that I can complain - I opted for VLc because of its forgiveness and multitude of native codecs. – Giulio Muscarello Mar 5 '14 at 16:24
You can save five characters by executing the command while the working directory is /dev. – David Richerby Mar 6 '14 at 7:15

Mathematica 52 - 5 = 47

g exports a white noise .wav file of s seconds and 8000 bps.


Example: a 6 second white noise file is exported.



share|improve this answer

Supercollider, 89 - 10% = 80.1 bytes

Sadly, despite being deliberately made for generation of sound/audio, this language is not going to win here. But it's the first appearance of Supercollider on Code Golf, so that's cool!

This submission loses primarily because setting up recording and making it happen is a verbose process owing to the client/server design of this language. Still, it's a cool language with a lot of power in very little code when you ask it to do things more complex than mere white noise.

File duration is set by changing the wait() value. I could put it in a variable, but there's really no point to that as Supercollider has no stdio to speak of. The interactivity is in manipulating the code live while the server is still playing. Essentially, the IDE is the I/O (unless you build a UI for your creation).

Here's the golfed version:


Here's a golfed version with the option to record in either aiff or wav, and specify a sample format (int16, int8, and float are all options). Unfortunately, even with all the bonuses, the version above fares better. This would be 139 - 30% = 97.3 bytes.


And here's an ungolfed version of the latter, so you can see what's going on.


share|improve this answer
one vote for use of Supercollider. There are a lot of other puzzles it'd be GREAT for. Check out music and audio – Not that Charles Mar 6 '14 at 15:58

Bash + ALSA, score:44 (52 chars - (10% + 5%) bonuses)

Longer than the other bash answer, but accepts duration and bitrate. Also adds a reasonably correct header to the file, so should be reasonably portable:

arecord -r$2|head -c44;head -c$[$2*$1] /dev/urandom

Save as a script, chmod +x it and run:

$ ./ 1 44100 > c.wav
Recording WAVE 'stdin' : Unsigned 8 bit, Rate 44100 Hz, Mono

Note, .wav output is to stdout, so it must be redirected to a file.

share|improve this answer
You could save about 1,20 characters by reading /dev/random instead of /dev/urandom, since speed is not a requirement. – Giulio Muscarello Mar 5 '14 at 21:14
output is to stdin? – immibis Mar 6 '14 at 0:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.