21
\$\begingroup\$

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!

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

17 Answers 17

7
\$\begingroup\$

MATL, 15 14 13 bytes

8tE2$r&S1=3YG

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(...)
\$\endgroup\$
6
\$\begingroup\$

Pyth - 16 15 bytes

Outputs image to o.png.

.wCm.S+255mZ7y8

Example image:

\$\endgroup\$
4
\$\begingroup\$

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
    for(
        srand(time(0)), c = 17;
        --c;
        
        // (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
    write(1,a,138);
}

Updates:

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

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)
\$\endgroup\$
3
\$\begingroup\$

Octave / MATLAB, 48 37 35 bytes

[~,z]=sort(rand(8,16));imshow(z==1)

Example (on Octave):

enter image description here

Explanation

           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
\$\endgroup\$
3
\$\begingroup\$

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

Explanation

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
\$\endgroup\$
2
\$\begingroup\$

HTML+JavaScript, 148 bytes

c=O.getContext`2d`;for(x=16;x--;){r=Math.random()*8|0;for(y=8;y--;)c.fillStyle=y-r?'#000':'#fff',c.fillRect(x,y,1,1)}
<canvas id=O width=16 height=8>

\$\endgroup\$
2
\$\begingroup\$

Befunge, 90 bytes

This generates a PBM file written to stdout.

>030#v_\2*>>?1v
:v9\$<^!:-1\<+<
|>p1+:78+`!
>"P",1..8.28*8*v
.1-\88+%9p:!#@_>1-::88+%9g:!!

Try it online!

Explanation

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

\$\endgroup\$
1
  • \$\begingroup\$ Your online version says (in debug): "Unsupported instruction '' (0x05) (maybe not Befunge-93?)". And output is string-numerical rather than an image. \$\endgroup\$
    – trejder
    Dec 6, 2022 at 12:38
1
\$\begingroup\$

Mathematica, 77 60 bytes

Image[{RandomInteger@7+1,#}->1&~Array~16~SparseArray~{8,16}]

Sample Output

enter image description here

Explanation

{RandomInteger@7+1,#}->1&~Array~16

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

ReplacePixelValue[Image@(a=Array)[0&~a~16&,8],{#,RandomInteger@7+1}->1&~a~16]
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

QBasic, 59 bytes

RANDOMIZE TIMER
SCREEN 9
FOR x=0TO 15
PSET(x,RND*8-.5)
NEXT

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\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\$ Oct 11, 2017 at 14:58
  • \$\begingroup\$ 194 bytes: import java.awt.*;v->{new Frame(){public void paint(Graphics g){int x=50,i=16,y;g.fillRect(x,x,i,8);for(g.setColor(Color.WHITE);i>0;y*=Math.random(),g.drawLine(x+i,x+y,x+i--,x+y))y=8;}}.show();} \$\endgroup\$ Sep 14, 2020 at 15:08
0
\$\begingroup\$

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
closepath
fill
1 1 1 setrgbcolor
16{0 rand 16 mod 0.5 sub moveto 1 0 rlineto stroke 1 0 translate}repeat
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 30 bytes

FOR I=0TO 15GPSET I,RND(8)NEXT
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\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\$ Dec 18, 2017 at 18:09
  • \$\begingroup\$ ah yes I forgot. I remember I adjusted it manually. Thanks :) \$\endgroup\$
    – remoel
    Dec 19, 2017 at 3:04
  • \$\begingroup\$ (and you can use -1 instead of vbWhite - full explanation of why here) \$\endgroup\$ Dec 19, 2017 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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