Poking พิกเซลแบบสุ่ม


20

งานของคุณง่าย: เขียนโปรแกรมที่จะแทนที่พิกเซลแบบสุ่มในสี่เหลี่ยมสีดำ 16px * 8px (ความกว้างคูณความสูง) ด้วยพิกเซลสีขาว

หลุมจะต้องสุ่มอย่างสม่ำเสมอและคุณควรส่งออกภาพ 16px คูณ 8 px โดยใส่พิกเซลสีขาว

แทนที่เพียง 1 พิกเซลต่อคอลัมน์ (รวม 16 พิกเซลที่ถูกแทนที่)

คุณไม่ต้องป้อนข้อมูลใด ๆ และคุณไม่สามารถพึ่งพาภาพที่จัดเก็บไว้ที่อื่นบนคอมพิวเตอร์

นี่คือเพื่อให้โปรแกรมที่มีจำนวนไบต์สั้นที่สุดชนะ!


1
เอาต์พุตควรเปลี่ยนเมื่อรันโปรแกรมหลายครั้งหรือไม่?
เควนติน

@Quentin ใช่มันควร
GracefulLemming

1
ที่เกี่ยวข้อง: แสดงพิกเซลสีแบบสุ่ม
sergiol

คำตอบ:


7

MATL , 15 14 13 ไบต์

8tE2$r&S1=3YG

ตัวอย่าง (กับคอมไพเลอร์ MATL ที่ทำงานบน MATLAB):

ป้อนคำอธิบายรูปภาพที่นี่

หรือลองที่MATL Online! (หากไม่ได้ทำงานครั้งแรกให้กด "Run" อีกครั้งหรือรีเฟรชหน้า) โปรดทราบว่ารูปภาพจะถูกปรับสัดส่วนโดยล่ามออนไลน์เพื่อให้มองเห็นภาพได้ดีขึ้น

นี่เป็นพอร์ตของคำตอบ Octave / MATLABของฉัน(ดูคำอธิบายที่นั่น) นี่คือข้อความที่เทียบเท่า:

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


3

ระดับแปดเสียง / MATLAB, 48 37 35 ไบต์

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

ตัวอย่าง (บนระดับแปดเสียง):

ป้อนคำอธิบายรูปภาพที่นี่

คำอธิบาย

           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

3

C, 85 100 ไบต์

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);}

เขียนไฟล์รูปภาพPGMไปยัง stdout (เรียกว่าด้วยprog >out.pgm)

Ungolfed และอธิบาย:

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);
}

ปรับปรุง:

  • OP ได้ชี้แจงว่าเอาต์พุตควรเปลี่ยนไปเมื่อการประมวลผลแต่ละครั้งหายไป 15 ไบต์เป็นsrand(time(0))( :()

3

กำลังประมวลผล74 73 ไบต์

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

ตัวอย่างผลลัพธ์:

ป้อนคำอธิบายรูปภาพที่นี่

คำอธิบาย

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

2

Ruby, 61 ไบต์

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

นี่เป็นโปรแกรมเต็มรูปแบบที่แสดงผลภาพในรูปแบบ netpbm ไปยัง stdout

ตัวอย่างผลลัพธ์:

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)

2

Befunge, 90 ไบต์

สิ่งนี้สร้างไฟล์ PBM ที่เขียนไปยัง stdout

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

ลองออนไลน์!

คำอธิบาย

สามบรรทัดแรกเป็นตัวสร้างหมายเลขสุ่มโดยเก็บหมายเลข 3 บิตแบบสุ่ม 16 บิต (เช่นในช่วง 0-7) บนบรรทัดที่สิบของสนามแข่งขัน บรรทัดที่สี่เขียนส่วนหัว PBM ออกจากนั้นบรรทัดสุดท้ายจะสร้างพิกเซลของภาพ สิ่งนี้ทำได้โดยการนับตัวเลขสุ่ม 16 ตัวเมื่อพิกเซลถูกส่งออก - เมื่อจำนวนที่สอดคล้องกับคอลัมน์ใดคอลัมน์หนึ่งถึงศูนย์เราจะส่งออก 1 แทน 0

ตัวอย่างเอาต์พุต (ซูม):

ตัวอย่างผลลัพธ์


1

Mathematica, 77 60 ไบต์

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

ตัวอย่างผลลัพธ์

ป้อนคำอธิบายรูปภาพที่นี่

คำอธิบาย

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

สร้างกฎการแทนที่สำหรับแต่ละคอลัมน์ แทนที่ตำแหน่งที่เลือกแบบสุ่มด้วย 1

... ~SparseArray~{8,16}

สร้าง a ที่SparseArrayมีขนาด 8x16 จากกฎการเปลี่ยน พื้นหลังเป็น0ค่าเริ่มต้น (8x16 เพราะ Mathematica นับแถวก่อน)

Image[ ... ]

แปลงSparseArrayเป็นImageวัตถุ

77 ไบต์

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


0

R, 76 ไบต์

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

ใช้แพคเกจpngเพื่อส่งออกไปยังไฟล์
ตัวอย่างผลลัพธ์:

ป้อนคำอธิบายรูปภาพที่นี่


0

QBasic, 59 ไบต์

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

ตรงไปตรงมาสวย -.5เป็นสิ่งจำเป็นเพราะPSETมีข้อโต้แย้งที่ไม่ใช่จำนวนเต็มใช้รอบเพื่อที่ใกล้ที่สุดแทนพื้นหรือตัด (และ-.5สั้นกว่าINT())

รูปภาพที่เป็นปัญหาจะปรากฏขึ้นที่มุมซ้ายบนของหน้าต่างเอาท์พุท ตัวอย่าง A (ครอบตัด):16 จุดสุ่ม


0

Java, ( ใช้กับ 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();}

เป็นเรื่องไร้สาระเพราะระบบพิกัดรวมถึงกรอบหน้าต่างเฟรม ... ดังนั้นคุณต้องบัฟเฟอร์อย่างน้อย 26 ไบต์หรือไม่มีอะไรปรากฏขึ้นดังนั้นx=50บิต


ฉันรู้ว่ามันรับในขณะที่ แต่คุณสามารถกอล์ฟไปยัง 238 ไบต์: 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();}(การเปลี่ยนแปลงทำ: คงเอาออก; Java 8 แลมบ์ดาบางints ลบออกi=16กลับมาใช้; นำสิ่งที่อยู่ภายในสำหรับวงที่จะลบวงเล็บและ;)
เควิน Cruijssen

0

Postscript (65 ไบต์)

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

เวอร์ชันที่ไม่ถูกปรับแต่ง:

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


0

ชิป -8, 12 ไบต์

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

วาดภาพบนหน้าจอ


0

Tcl / Tk, 163

สองวิธีที่ต่างกันทำให้มีขนาดเท่ากัน:

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

ป้อนคำอธิบายรูปภาพที่นี่


0

VBA Excel, 86 105 ไบต์

ใช้หน้าต่างทันที

Cells.RowHeight=42:[a1:p8].interior.color=0:for x=0to 15:[a1].offset(rnd*7,x).interior.color=vbwhite:next

วิธีการแก้ปัญหานี้น่าเสียดายที่ไม่ได้ทำให้เซลล์ของจตุรัสแผ่นงานตามที่จำเป็นสำหรับโซลูชันศิลปะพิกเซล VBA ของ Excel - โซลูชันที่ถูกต้องมากขึ้นคือCells.RowHeight=42:[A1:P8].Interior.Color=0:For x=0To 15:[A1].Offset(Rnd*7,x).Interior.Color=-1:Next
Scott Scott

อ่าใช่ฉันลืม ฉันจำได้ว่าฉันปรับมันด้วยตนเอง ขอบคุณ :)
remoel

(และคุณสามารถใช้-1แทนvbWhite- คำอธิบายที่สมบูรณ์ว่าทำไมที่นี่ )
Taylor Scott
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.