C, 85 100 byte
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);}
Menulis file gambar PGM ke stdout (panggil saja prog >out.pgm).
Tidak digabungkan dan dijelaskan:
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);
}
Pembaruan:
- OP telah mengklarifikasi bahwa output harus berubah dengan setiap eksekusi, kehilangan 15 byte menjadi
srand(time(0))( :()