Your task is simple: write a program that will replace random pixels in a black 16px * 8px rectangle (width by height) with a white pixel.

The holes must be uniformly random, and you should output the 16px by 8 px image with the white pixels inserted.

Replace only 1 pixel per column (16 total replaced pixels)

You don't take any input, and you can't rely on the image being stored elsewhere on the computer.

This is so the program with the shortest bytecount wins!

  • 1
    \$\begingroup\$ Should the output change when executing the program multiple times? \$\endgroup\$ – Quentin Dec 26 '16 at 17:25
  • \$\begingroup\$ @Quentin yes it should \$\endgroup\$ – GracefulLemming Dec 26 '16 at 18:19
  • 1
    \$\begingroup\$ Related: Display random colored pixels \$\endgroup\$ – sergiol Oct 29 '17 at 22:22

17 Answers 17


MATL, 15 14 13 bytes


Example (with MATL compiler running on MATLAB):

enter image description here

Or try it at MATL Online! (If it doesn't run the first time, press "Run" again or refresh the page). Note that the image is scaled by the online interpreter for better visualization.

This is a port of my Octave / MATLAB answer (see explanation there). Here are the equivalent statements:

MATL        Octave / MATLAB
----        ---------------
8tE         8,16
2$r         rand(...)
&S          [~,out]=sort(...)
1=          ...==1 
3YG         imshow(...)

Pyth - 16 15 bytes

Outputs image to o.png.


Example image:


Octave / MATLAB, 48 37 35 bytes


Example (on Octave):

enter image description here


           rand(8,16)                  % 8×16 matrix of random values with uniform
                                       % distribution in (0,1)
[~,z]=sort(          );                % Sort each column, and for each output the
                                       % indices of the sorting. This gives an 8×16
                                       % matrix where each column contains a random
                                       % permutation of the values 1,2,...,8 
                              z==1     % Test equality with 1. This makes all values 
                                       % except 1 equal to 0
                       imshow(    )    % Show image, with grey colormap

C, 85 100 bytes

main(c){char a[138]="P5 16 8 1 ";for(srand(time(0)),c=17;--c;a[9+c+(rand()&112)]=1);write(1,a,138);}

Writes a PGM image file to stdout (call it with prog >out.pgm).

Ungolfed and explained:

main(c) {
    // A buffer large enough to contain the whole image,
    // pre-filled with the PGM header.
    // This is a binary greyscale (P5) image with only two levels (1),
    // Because a binary bitmap would require pixel packing.
    char a[138] = "P5 16 8 1 ";

    // c iterates from 16 to 1 over the columns
        srand(time(0)), c = 17;

        // (rand() % 8) * 16 is equivalent to (rand() & 7) << 4
        // Since all bits are equally random, this is equivalent
        // to rand() & (7 << 4), that is rand() & 112.
        // This picks a pixel from the first column, which is then
        // offset to the correct column by c - 1 + 10
        // (10 is the length of the header).
        a[9 + c + (rand() & 112)] = 1
        ; // Empty for body

    // Write the whole buffer to stdout


  • OP has clarified that the output should change with each execution, lost 15 bytes to srand(time(0)) (:()

Processing, 74 73 bytes

fill(0);rect(0,0,15,7);stroke(-1);for(int i=0;i<16;)point(i++,random(8));

Sample output:

enter image description here


fill(0);               //sets the fill colour to black
rect(0,0,15,7);        //draws a 16*8 black rectangle
stroke(-1);            //set stroke colour to white
for(int i=0;i<16;)     // for-loop with 16 iterations
 point(i++,random(8)); //  draw a point at x-coordinate i and a random y coordinate
                       //  the colour of the point is white since that is the stroke colour

Ruby, 61 bytes

puts'P116 8';puts Array.new(16){[*[1]*7,0].shuffle}.transpose

This is a full program that outputs the image in netpbm format to stdout.

Sample output:

puts'P116 8';   # output the netpbm header (P1 for black and white, 16x8)
puts            # then output the data as follows:
Array.new(16){  # make a 16-element array and for each element,
[*[1]*7,0]      # generate the array [1,1,1,1,1,1,1,0] (1=black 0=white)
.shuffle}       # shuffle the array
.transpose      # transpose the rows/columns of the 2d array (so that each column
                # has one white pixel)

Befunge, 90 bytes

This generates a PBM file written to stdout.


Try it online!


The top three lines make up the random number generator, storing 16 random 3-bit numbers (i.e. in the range 0-7) on the tenth line of the playfield. Line four writes out the PBM header, and the last line then generates the pixels of the image. This is done by counting down the 16 random numbers as the pixels are output - when the number corresponding to a particular column reaches zero we output a 1 rather than a 0.

Sample output (zoomed):

Sample output


Mathematica, 77 60 bytes


Sample Output

enter image description here



Make replacement rules for each column; replace a randomly selected position with 1.

... ~SparseArray~{8,16}

Create a SparseArray with size 8x16 from the replacement rules. The background is 0 by default. (8x16 because Mathematica counts rows first)

Image[ ... ]

Convert the SparseArray into an Image object.

77 byte version


HTML+JavaScript, 148 bytes

<canvas id=O width=16 height=8>


R, 76 bytes

a=matrix(0,8,16);for(i in 1:16)a[sample(1:8,1),i]=1;png::writePNG(a,'a.png')

Uses package png to output to a file.
Example output:

enter image description here


QBasic, 59 bytes

FOR x=0TO 15

Pretty straightforward. The -.5 is needed because PSET with non-integer arguments uses round-to-nearest instead of floor or truncate (and -.5 is shorter than INT()).

The image in question is displayed at the top left corner of the output window. A (cropped) example: 16 random dots


Java, (Does it even matter Bytes, AKA 244 + 18 import = 262)

import java.awt.*;static void l(){new Frame(){public void paint(Graphics g){int x=50;int i=16;g.setColor(Color.BLACK);g.fillRect(x,x,16,8);g.setColor(Color.WHITE);for(;i>0;i--){int y=(int)(Math.random()*8);g.drawLine(x+i,x+y,x+i,x+y);setVisible(1>0);}}}.show();}

Was wonky because the coordinate system includes the frame window pane... So you need to buffer by at least 26 bytes or nothing shows up, hence the x=50 bit.

  • \$\begingroup\$ I know it's been a while, but you can golf it to 238 bytes: import java.awt.*;v->{new Frame(){public void paint(Graphics g){int x=50,i=16,y;g.setColor(Color.BLACK);g.fillRect(x,x,i,8);for(g.setColor(Color.WHITE);i>0;g.drawLine(x+i,x+y,x+i--,x+y),setVisible(1>0))y=(int)(Math.random()*8);}}.show();} (Changes done: static removed; Java 8 lambda; some ints removed; i=16 reused; put things inside the for-loop to remove brackets and ;) \$\endgroup\$ – Kevin Cruijssen Oct 11 '17 at 14:58

Postscript (65 bytes)

0 0íkà0ícà8íc0 8ícííB1à1íù16{0 randàíj0.5í©ík1 0íÖíß1 0í≠}íÉ

Ungolfed version:

0 0 moveto
16 0 lineto
16 8 lineto
0 8 lineto
1 1 1 setrgbcolor
16{0 rand 16 mod 0.5 sub moveto 1 0 rlineto stroke 1 0 translate}repeat

SmileBASIC, 30 bytes


Chip-8, 12 bytes

0xA201 'Load sprite at address 201 (which is the second byte of this instruction)
0xC007 'Set register 0 to a random number from 0 to 7 (rnd & 0x7)
0xD101 'Draw sprite. x = register 1, y = register 0, height = 1
0x7101 'Add 1 to register 1
0x3110 'If register 1 is not 16...
0x1202 'Jump to second instruction

Draws the image on the screen.


Tcl/Tk, 163

Two different approaches render to the same byte extent:

grid [canvas .c -w 16 -he 8 -bg #000 -highlightt 0]
.c cr i 8 4 -i [set p [image c photo -w 16 -h 8]]
set x 0
time {$p p #FFF -t $x [expr int(rand()*8)];incr x} 16

grid [canvas .c -w 16 -he 8 -bg #000 -highlightt 0]
.c cr i 8 4 -i [set p [image c photo -w 16 -h 8]]
time {$p p #FFF -t [expr [incr x]-1] [expr int(rand()*8)]} 16

enter image description here


VBA Excel, 86 105 bytes

using immediate window

Cells.RowHeight=42:[a1:p8].interior.color=0:for x=0to 15:[a1].offset(rnd*7,x).interior.color=vbwhite:next
  • \$\begingroup\$ This solution unfortunately does not make the cells of the activesheet square, as is required for Excel VBA pixel art solutions - A more valid solution would be Cells.RowHeight=42:[A1:P8].Interior.Color=0:For x=0To 15:[A1].Offset(Rnd*7,x).Interior.Color=-1:Next \$\endgroup\$ – Taylor Scott Dec 18 '17 at 18:09
  • \$\begingroup\$ ah yes I forgot. I remember I adjusted it manually. Thanks :) \$\endgroup\$ – remoel Dec 19 '17 at 3:04
  • \$\begingroup\$ (and you can use -1 instead of vbWhite - full explanation of why here) \$\endgroup\$ – Taylor Scott Dec 19 '17 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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