49
\$\begingroup\$

I've always liked screens full of randomly colored pixels. They're interesting to look at and the programs that draw them are fun to watch.

The challenge

Fill your screen, or a graphical window, with colored pixels.

The rules

  • Your program must have an even chance of picking all colors (i.e in the range #000000 to #FFFFFF), or all colors that can be displayed on your system.
  • Your program must continue to display random pixels until manually stopped (it cannot terminate on its own).
  • Pixels can be any size, as long as your output has at least 40x40 "pixels".
  • Your program must run at such a speed that it can replace every pixel on the screen/window at least once after running for three minutes.
  • Your program must choose truly random colors and points to replace, i.e. random with all points/colors equally likely. It cannot just look random. It must use a pRNG or better, and the output cannot be the same every time.
  • Your program must have an equal chance of picking all colors each iteration.
  • Your program must replace only one pixel at once.
  • Your program cannot use the internet nor your filesystem (/dev/random and /dev/urandom excepted).

Example

Your output could look like this if stopped at a random time:

The winner

The shortest answer in each language wins. Have fun!

\$\endgroup\$
8
  • \$\begingroup\$ By "truly random", I assume pseudorandom is OK (like Math.random() or similar) \$\endgroup\$
    – AAM111
    Jun 2, 2017 at 1:29
  • \$\begingroup\$ @OldBunny2800 It must use a pRNG or better \$\endgroup\$ Jun 2, 2017 at 13:07
  • \$\begingroup\$ Since the program can't use the internet, I'll just leave this in a comment: babelia.libraryofbabel.info/slideshow.html \$\endgroup\$
    – KSmarts
    Jun 2, 2017 at 13:28
  • 1
    \$\begingroup\$ So does the "3 minute limit" apply to setup time, too? Or just once the "program" begins? Asking because I have a Minecraft redstone solution, which runs very quickly once it is going, but takes a while to set up initially (it has to "place" each "pixel" in the 40x40 grid) before it can start changing colors. \$\endgroup\$
    – BradC
    Jun 4, 2017 at 2:34
  • 5
    \$\begingroup\$ Truly random by definition exclude pseudo-random, yet doesn't define a distribution. I assume you mean a uniform PRNG, where all events are equiprobable and independent of each other. \$\endgroup\$
    – Dennis
    Jun 4, 2017 at 4:44

50 Answers 50

1
2
2
\$\begingroup\$

Minecraft 1.12.2, 747 bytes(compressed), 836+60(contain+filename)

0000h: 52 61 72 21 1A 07 01 00 F3 E1 82 EB 0B 01 05 07
0010h: 00 06 01 01 80 80 80 00 8A 2C 8F 18 25 02 03 0B
0020h: 94 01 04 94 01 20 D2 9C B3 95 80 00 00 09 6C 65
0030h: 76 65 6C 2E 64 61 74 0A 03 02 F0 E3 51 1A 04 6E
0040h: D4 01 1F 8B 08 00 34 EB D4 5B 00 FF E3 62 60 E0
0050h: 62 60 71 49 2C 49 E4 60 E0 4D 4F CD 4B 2D 4A 2C
0060h: C9 2F F2 4B CC 4D 65 60 49 CB 49 2C 61 66 E0 70
0070h: 07 72 42 2A 0B 52 19 18 18 98 99 19 D8 CB 52 8B
0080h: 8A 33 F3 F3 18 18 BC F6 72 31 70 82 24 83 4A 73
0090h: 52 8B 39 18 04 D2 81 6C 9F FC FC 02 B7 D2 BC E4
00a0h: 12 90 12 C6 44 06 66 06 B6 E0 82 C4 F2 BC 28 06
00b0h: 90 76 28 27 02 C4 01 69 80 D9 E7 5F 00 52 5F CC
00c0h: C0 69 6C 6D 64 6A A6 65 68 6D C8 C0 00 00 20 45
00d0h: 88 30 9A 00 00 00 C6 A9 60 BA 41 02 03 0B 92 01
00e0h: 04 A9 02 20 58 56 55 04 80 03 00 25 64 61 74 61
00f0h: 2F 66 75 6E 63 74 69 6F 6E 73 2F 6D 69 6E 65 63
0100h: 72 61 66 74 2F 61 2E 6D 63 66 75 6E 63 74 69 6F
0110h: 6E 0A 03 02 76 DA 2D 12 05 6E D4 01 C6 13 8F 20
0120h: 54 43 33 F6 50 34 BD 83 E0 EA 70 08 12 22 42 4A
0130h: 60 4E 1F 06 31 44 F5 04 06 DA 42 5A 88 DD 2F BB
0140h: BD 07 AD 36 E9 75 3E 23 86 E9 A6 7C 51 B9 C9 72
0150h: 8C 30 CF 07 83 D9 46 51 EB 3D C3 E1 1B 2C 6D A8
0160h: 53 C1 54 83 18 0B 1A 57 5B 63 54 F3 41 D9 97 53
0170h: AB 55 E0 F8 23 A6 81 32 D7 B0 F8 37 B8 3D 72 D5
0180h: 3A 27 98 2E 39 A2 20 68 BC BA DB FE 84 61 A0 17
0190h: 19 D0 97 3C FA 9E 71 98 92 87 6C 7C 93 DF E5 D9
01a0h: AC 8D AB 1F C7 4C FB 91 29 D4 59 3E 3B 90 1B C0
01b0h: AA 3A 41 02 03 0B E8 00 04 87 03 20 19 8F C5 65
01c0h: 80 03 00 25 64 61 74 61 2F 66 75 6E 63 74 69 6F
01d0h: 6E 73 2F 6D 69 6E 65 63 72 61 66 74 2F 62 2E 6D
01e0h: 63 66 75 6E 63 74 69 6F 6E 0A 03 02 22 1C 99 C6
01f0h: 02 6E D4 01 C6 F9 65 26 43 54 2F 76 04 3E E8 34
0200h: 3E D0 28 3E DF E5 3A A3 C9 D0 DE 0A 0F 3D BE 27
0210h: 9F C7 2F 2E B4 CC E1 30 81 32 A5 81 33 0C A0 F2
0220h: 43 EA 5B 6D 37 CA 74 CF 55 55 55 C0 BC 9E 29 49
0230h: A8 9D 7D DF 02 97 B4 1E B6 60 16 80 58 18 31 01
0240h: A3 62 07 0A 46 3C 51 E4 80 A0 08 89 E0 90 AD 7F
0250h: DF 2E AF CE 23 A6 91 E3 48 FA D0 1C BA A1 75 20
0260h: 32 02 03 0B 00 05 00 10 00 00 00 00 80 00 00 18
0270h: 64 61 74 61 2F 66 75 6E 63 74 69 6F 6E 73 2F 6D
0280h: 69 6E 65 63 72 61 66 74 0A 03 02 AD C3 9B 2D 04
0290h: 6E D4 01 C4 B1 B7 E8 28 02 03 0B 00 05 00 10 00
02a0h: 00 00 00 80 00 00 0E 64 61 74 61 2F 66 75 6E 63
02b0h: 74 69 6F 6E 73 0A 03 02 D1 6A 8E 26 25 6D D4 01
02c0h: F6 87 4C DB 1E 02 03 0B 00 05 00 10 00 00 00 00
02d0h: 80 00 00 04 64 61 74 61 0A 03 02 EE 28 D1 1F 25
02e0h: 6D D4 01 1D 77 56 51 03 05 04 00               

To prove whole world is not always bigger (bin is .rar file)

level.dat
    Data
        GameRules
            gameLoopFunction: a
        GameType: 3 # so you don't fall and mob won't spawn at night
        generatorName: flat
        generatorOptions: 3;256*1;1 # full of stone
        SpawnX: 0
        SpawnZ: 0
        version: 19133 # avaliable
data
    functions
        minecraft
            a.mcfunction
                summon area_effect_cloud
                # won't show something when die
                spreadplayers 0 0 0 20 0 @e[type=!player]
                execute @r[type=!cow] ~ ~ ~ tp @s ~ ~-1 ~
                execute @r[type=!cow] ~ ~ ~ tp @s ~ ~-2 ~
                execute @r[type=!cow] ~ ~ ~ tp @s ~ ~-4 ~
                execute @r[type=!cow] ~ ~ ~ tp @s ~ ~-8 ~
                # minecraft bug, tp @r[type=!cow] ~ ~-1 ~
                # always tp player
                execute @e[type=!player] ~ ~ ~ function b
                tp @a ~ 275 ~
            b.mcfunction
                setblock ~ ~-1 ~ wool 15
                setblock ~ ~ ~ wool 14
                setblock ~ ~1 ~ wool 13
                setblock ~ ~2 ~ wool 12
                setblock ~ ~3 ~ wool 11
                setblock ~ ~4 ~ wool 10
                setblock ~ ~5 ~ wool 9
                setblock ~ ~6 ~ wool 8
                setblock ~ ~7 ~ wool 7
                setblock ~ ~8 ~ wool 6
                setblock ~ ~9 ~ wool 5
                setblock ~ ~10 ~ wool 4
                setblock ~ ~11 ~ wool 3
                setblock ~ ~12 ~ wool 2
                setblock ~ ~13 ~ wool 1
                setblock ~ ~14 ~ wool

Minecraft 1.12.2, 806 bytes(compressed), 1352+60(contain+filename)

0000h: 52 61 72 21 1A 07 01 00 F3 E1 82 EB 0B 01 05 07
0010h: 00 06 01 01 80 80 80 00 33 72 E7 9F 41 02 03 0B
0020h: 9E 01 04 EF 02 20 EC 92 16 D1 80 03 00 25 64 61
0030h: 74 61 2F 66 75 6E 63 74 69 6F 6E 73 2F 6D 69 6E
0040h: 65 63 72 61 66 74 2F 61 2E 6D 63 66 75 6E 63 74
0050h: 69 6F 6E 0A 03 02 E4 73 98 96 26 6D D4 01 C3 02
0060h: 9B 26 54 34 33 F6 60 34 BD 83 E0 EA 70 08 12 39
0070h: 28 94 C0 7C 3E 10 66 FE 16 83 86 B6 99 BF 88 E6
0080h: D3 3B BB 70 16 9A C8 4B E7 0E 41 82 48 CF 65 19
0090h: C9 73 C2 CC A1 21 60 7A 8F 07 DC 61 F1 C3 02 C4
00a0h: 10 E1 45 C2 C6 0A A1 72 9A 51 5D 75 32 F6 B4 37
00b0h: D9 C0 39 51 69 A4 D3 32 26 D7 53 D4 46 24 91 91
00c0h: 60 49 F1 31 DF E4 5E 70 D6 7D 20 17 39 24 6C 1F
00d0h: 47 FA 30 10 B5 DD A1 2C 87 AE E3 0C FB 87 BA 67
00e0h: E9 72 6A 24 2E D8 EB 5F F2 E3 FC 1B 66 9C F4 E9
00f0h: AA 3A FB 3C FA 7B 23 89 D4 8E E8 90 4C 1E F8 90
0100h: 41 02 03 0B 9C 01 04 CA 06 20 D0 72 7A 6E 80 03
0110h: 00 25 64 61 74 61 2F 66 75 6E 63 74 69 6F 6E 73
0120h: 2F 6D 69 6E 65 63 72 61 66 74 2F 62 2E 6D 63 66
0130h: 75 6E 63 74 69 6F 6E 0A 03 02 5E 12 89 DC 26 6D
0140h: D4 01 C7 04 99 26 54 33 33 F6 60 44 DF 42 F0 77
0150h: C1 1B 71 D9 F8 D1 42 F0 FA 52 8E BA B5 56 0E 0D
0160h: 4A 5B 74 DE F1 9A 23 93 54 07 17 13 42 46 02 5E
0170h: D4 BD C8 F3 F2 96 06 70 5F 7B EE 24 78 CC 9F E7
0180h: 18 BF F1 4C D4 BD 69 BA 63 A1 F1 E0 5F 4A 1E F2
0190h: 2A 4D 5F D4 EA 59 96 5E 23 28 7A A4 B4 AF 92 94
01a0h: 76 74 EA 70 94 CB BB F9 B3 97 64 00 AC F6 EE 0D
01b0h: C1 B8 65 81 D8 31 A1 B4 63 03 60 C5 86 B1 78 24
01c0h: 2C 2B 86 F0 AF 17 70 57 0B B4 2B 45 A4 85 AE 87
01d0h: A4 87 57 42 24 83 AE 82 49 03 B8 80 4B E6 66 BD
01e0h: 96 D5 25 02 03 0B 8F 01 04 8F 01 20 83 7C 6D FD
01f0h: 80 00 00 09 6C 65 76 65 6C 2E 64 61 74 0A 03 02
0200h: 10 70 3C 51 27 6D D4 01 1F 8B 08 00 C9 78 D3 5B
0210h: 00 FF 35 8D 41 0A C2 30 14 44 47 D2 6A 1A 28 78
0220h: 14 C5 65 B7 62 41 A4 05 75 21 EE 3E F2 5B 84 9A
0230h: 84 24 55 3C 9C 77 B3 1F 74 37 8F 79 C3 18 C0 20
0240h: DB 52 22 8D B2 67 CB 81 92 0B 0D 3D 18 59 37 50
0250h: 52 D0 F5 04 E7 B7 67 00 4A 61 F1 E4 10 EF CE 02
0260h: FB 8F 41 21 E5 71 1C 38 6A 2C FB 29 1F 9C F3 BB
0270h: D1 DE 92 28 33 82 C2 FC E4 E9 65 AF 90 F9 0F 2E
0280h: 02 32 F8 FF B5 5E FC 88 7C 53 AD AA 35 F0 05 F9
0290h: 41 3F 00 96 00 00 00 1B 7A 52 1B 32 02 03 0B 00
02a0h: 05 00 10 00 00 00 00 80 00 00 18 64 61 74 61 2F
02b0h: 66 75 6E 63 74 69 6F 6E 73 2F 6D 69 6E 65 63 72
02c0h: 61 66 74 0A 03 02 BB 8F 67 2F 25 6D D4 01 C4 B1
02d0h: B7 E8 28 02 03 0B 00 05 00 10 00 00 00 00 80 00
02e0h: 00 0E 64 61 74 61 2F 66 75 6E 63 74 69 6F 6E 73
02f0h: 0A 03 02 D1 6A 8E 26 25 6D D4 01 F6 87 4C DB 1E
0300h: 02 03 0B 00 05 00 10 00 00 00 00 80 00 00 04 64
0310h: 61 74 61 0A 03 02 EE 28 D1 1F 25 6D D4 01 1D 77
0320h: 56 51 03 05 04 00                              



level.dat
    Data
        GameRules
            gameLoopFunction: a
        GameType: 3 # so you don't fall and mob won't spawn at night
        generatorName: flat
        generatorOptions: 3;0;1 # just air
        SpawnX: 0
        SpawnZ: 0
        version: 19133 # avaliable
data
    functions
        minecraft
            a.mcfunction
                summon area_effect_cloud
                # won't show something when die
                fill -20 3 -20 20 3 20 wool
                # init. A .mca is at least 12KB too expensive
                # (though flat generation may work fine)
                scoreboard objectives add a dummy
                execute @r[type=!cow] ~ ~ ~ scoreboard players add @s a 1
                execute @r[type=!cow] ~ ~ ~ scoreboard players add @s a 2
                execute @r[type=!cow] ~ ~ ~ scoreboard players add @s a 4
                execute @r[type=!cow] ~ ~ ~ scoreboard players add @s a 8
                # minecraft bug, scoreboard players add @r[type=!cow] a 1
                # always add score to player
                execute @e[type=!player] ~ ~ ~ function b
            b.mcfunction
                spreadplayers 0 0 0 20 0 @s
                execute @s ~ 4 ~ setblock ~ ~ ~ wool
                # setblock ~ 4 ~ wool uses the old location
                execute @s[score_a=15] ~ 4 ~ setblock ~ ~ ~ wool 15
                execute @s[score_a=14] ~ 4 ~ setblock ~ ~ ~ wool 14
                execute @s[score_a=13] ~ 4 ~ setblock ~ ~ ~ wool 13
                execute @s[score_a=12] ~ 4 ~ setblock ~ ~ ~ wool 12
                execute @s[score_a=11] ~ 4 ~ setblock ~ ~ ~ wool 11
                execute @s[score_a=10] ~ 4 ~ setblock ~ ~ ~ wool 10
                execute @s[score_a=9] ~ 4 ~ setblock ~ ~ ~ wool 9
                execute @s[score_a=8] ~ 4 ~ setblock ~ ~ ~ wool 8
                execute @s[score_a=7] ~ 4 ~ setblock ~ ~ ~ wool 7
                execute @s[score_a=6] ~ 4 ~ setblock ~ ~ ~ wool 6
                execute @s[score_a=5] ~ 4 ~ setblock ~ ~ ~ wool 5
                execute @s[score_a=4] ~ 4 ~ setblock ~ ~ ~ wool 4
                execute @s[score_a=3] ~ 4 ~ setblock ~ ~ ~ wool 3
                execute @s[score_a=2] ~ 4 ~ setblock ~ ~ ~ wool 2
                execute @s[score_a=1] ~ 4 ~ setblock ~ ~ ~ wool 1
\$\endgroup\$
5
  • \$\begingroup\$ So, what is this, exactly? Is this a compressed version of the structure block from my answer? Or your own program/save file? \$\endgroup\$
    – BradC
    Oct 26, 2018 at 13:40
  • \$\begingroup\$ @BradC the whole map with level.dat etc. \$\endgroup\$
    – l4m2
    Oct 26, 2018 at 13:49
  • \$\begingroup\$ Also, no structure block, no reading your solution \$\endgroup\$
    – l4m2
    Oct 26, 2018 at 13:56
  • \$\begingroup\$ Cool, a new independent Minecraft solution, then. I'd love to see more about how yours works, its a little hard to tell from a block of raw binary :) \$\endgroup\$
    – BradC
    Oct 26, 2018 at 14:03
  • \$\begingroup\$ @BradC added explanation \$\endgroup\$
    – l4m2
    Oct 26, 2018 at 14:13
2
\$\begingroup\$

Commodore 64 (old ROM I think), ~28 tokenized BASIC Bytes used

 0poke646,rnd(.)*16:?"{reverse on} ";:goto

The memory location 646 is used as a colour out in the BASIC kernal (pretty sure this is the old kernal, not the C64c kernal), which is one of the 16 colours available in its palette. Then a reverse space is printed after. goto is used as an infinite loop until the screen is filled.

This is the cheapest in terms of bytes. There is a nicer way to do this, as follows:

Commodore 64 (new and old ROM), ~46 tokenized BASIC bytes

 0 fori=0to999:poke1024+i,160:poke55296+i,rnd(.)*16:next

This will poke firstly a reversed (or inversed) space to the screen RAM (at 0x0400 - 0x07e7), and then alter the colour attribute for each character position (mapped at 0xd800 - 0xdbe7) between 0 and 15 inclusive.

As soon as the for/next loop ends, the ready. prompt is shown. To stop this, add the following line, costing another 7 tokenized BASIC bytes:

 1 go to 1

Commodore C64 random colour generator simulator

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

Powershell, 128 126 124 88 bytes

-2 bytes thanks @Veskah

1..($c=[Console])::WindowWidth*$c::WindowHeight|%{Write-Host ' '-b(random 16)-n}
for(){}

Output:

console

VS code output:

VS Code

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Don't need the semi-colon and there's an extra space in the write-host \$\endgroup\$
    – Veskah
    May 8, 2019 at 13:22
  • 1
    \$\begingroup\$ not sure this satisfies the condition of overwriting every cell at least once within 3 minutes \$\endgroup\$
    – T3RR0R
    Oct 28, 2021 at 19:33
  • \$\begingroup\$ Thanks. Fixed. ¯\_(ツ)_/¯ \$\endgroup\$
    – mazzy
    Oct 28, 2021 at 20:01
  • \$\begingroup\$ Your latest edit is not functional for the terms of the challenge. \$\endgroup\$
    – T3RR0R
    Oct 28, 2021 at 21:08
  • 1
    \$\begingroup\$ A golf of your last working answer that brings it down to 105 bytes and ensures all cells can be rewritten within 3 minutes as required: mode 40,40;for(){[Console]::SetCursorPosition((random(40)),(random(40)));Write-Host ' '-b(random 16)-n} \$\endgroup\$
    – T3RR0R
    Oct 30, 2021 at 12:40
2
\$\begingroup\$

TI-Basic (83/84(+(SE)) only), 33 bytes

For(I,0,62
For(J,0,94
If int(2rand
Pxl-Change(I,J
End
End
prgmC

where C is the name of the program.

Without calling itself (+2 bytes):

While 1
For(I,0,62
For(J,0,94
If int(2rand
Pxl-Change(I,J
End
End
End

The TI-83 family calculators with monochrome screens (TI-83, TI-83+, TI-83+SE, TI-84+, and TI-84+SE) support only black and white as colors. This program goes through all the pixels on the screen (except for the rightmost column and bottommost row) and has a 50% chance of inverting it. This may take more than 3 minutes depending on the calculator; if so, replacing 62 and 94 with 39 each should work.

\$\endgroup\$
1
\$\begingroup\$

R, 143 138 bytes

-4 bytes thanks to user2390246

r=runif
s=sample
g=1600
x=1:40
k=hsv(r(g),r(g),r(g))
while(1){image(x,x,matrix(1:g,40),c=k)
Sys.sleep(99/g)
k[s(g,1)]=hsv(r(1),r(1),r(1))}

image makes the required 40x40 grid of rectangles with a matrix m of values (fixed to be 1:1600 in a 40x40 square), with a randomly selected color for each rectangle, k. It plots the image, waits long enough to actually render the image, then randomly changes one of the colors. Uses hsv encoding since it is a value between 0 and 1, which allows us to use runif to generate random colors.

This generates a new plot each time.

\$\endgroup\$
3
  • \$\begingroup\$ It won't affect the score, but shouldn't image(x,x,m,c=k) be outside the while loop? \$\endgroup\$ Jun 2, 2017 at 12:31
  • \$\begingroup\$ I don't think you need to define m, you only use it once. \$\endgroup\$ Jun 2, 2017 at 12:34
  • 1
    \$\begingroup\$ @user2390246 for the image question, nah, it has to be re-called every time since it's really drawing a new image each time after changing one of the colors. Good call on the m, though. I originally thought I had to change that matrix and never went back and changed it. \$\endgroup\$
    – Giuseppe
    Jun 2, 2017 at 13:17
1
\$\begingroup\$

Java 7 with JavaFX, 686 674 bytes

import javafx.scene.*;import javafx.scene.canvas.*;import javafx.scene.layout.*;public class M extends javafx.application.Application{public static void main(String[]a){launch(a);}GraphicsContext g;public M(){}public void start(javafx.stage.Stage x){BorderPane r=new BorderPane();Canvas c=new Canvas(40,40);g=c.getGraphicsContext2D();Pane p=new Pane();p.getChildren().addAll(c);r.setCenter(p);x.setScene(new Scene(r,40,40));x.show();new javafx.animation.AnimationTimer(){public void handle(long n){M.this.g.setFill(javafx.scene.paint.Color.color(Math.random(),Math.random(),Math.random()));M.this.g.fillRect((int)(Math.random()*40),(int)(Math.random()*40),1,1);}}.start();}}

Explanation:

import javafx.scene.*;import javafx.scene.canvas.*;import javafx.scene.layout.*;
                                            // Required imports

public class M extends javafx.application.Application{
                                            // The class (NOTE: The public is mandatory for JavaFX)
  public static void main(String[]a){       //  Mandatory main method
    launch(a);                              //   Call the launch of the Application
  }                                         //  End of main-method

  GraphicsContext g;                        //  GraphicsContext on class-level which we'll use in the animation

  public M(){}                              //  Empty constructor (again, mandatory for JavaFX)

  public void start(javafx.stage.Stage x){  //  Overwritten start-method of Application
    BorderPane r=new BorderPane();          //   Create a BorderPane
    Canvas c=new Canvas(40,40);             //   Create a Canvas with the minimum size
    g=c.getGraphicsContext2D();             //   Retrieve the GraphicsContext and set it to the class-level variable
    Pane p=new Pane();                      //   Create a Pane
    p.getChildren().addAll(c);              //   Add the Canvas to the Pane
    r.setCenter(p);                         //   Put it in the center (otherwise it won't show in the window)
    x.setScene(new Scene(r,40,40));         //   Create a new Scene
    x.show();                               //   And show the screen
    new javafx.animation.AnimationTimer(){  //   Create a new animation
      public void handle(long n){           //    Overwritten handle-method of this animation
        M.this.g.setFill(javafx.scene.paint.Color.color(Math.random(),Math.random(),Math.random()));
                                            //     Set a random color
        M.this.g.fillRect((int)(Math.random()*40),(int)(Math.random()*40),1,1);
                                            //     And add this color as a 1x1 pixel in a random position
      }                                     //    End of handle-method
    }.start();                              //   And start the animation
  }                                         //  End of start-method
}                                           // End of class

Output:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Mathematica 112 Bytes

r:=RandomReal[];b:=⌈40r⌉;a=r&~Array~{40,40,3};Dynamic[a[[b,b]]={r,r,r};ArrayPlot[a,ColorFunction->RGBColor]]

Very straightforward, uses Dynamic around an ArrayPlot. Not terribly fast; limited by the maximum speed dynamic can update, but should fulfill the goal on most computers.

Starts out with a random 40x40 grid and recolors a random pixel one at a time. Ceiling characters are 3 bytes each, Ouch!

\$\endgroup\$
1
\$\begingroup\$

Ruby + ruby2d: 101+9 = 110 bytes

Uses the -rruby2d flag. 80x60 "pixel" image. Timed the module to run 100 update ticks in 2 seconds, so it theoretically replaces all pixels on the screen in under 2 minutes.

s=[]
80.times{|x|60.times{|y|s<<Square.new(x:x*8,y:y*8,size:8)}}
update{s.sample.color='random'}
show
\$\endgroup\$
1
\$\begingroup\$

Processing.js, 142 bytes

for(var i=0;i<99;i++){for(var j=0;j<99;j++){fill(random(0,255),random(0,255),random(0,255),random(0,255));rect((i-1)*40,(j-1)*40,i*40,j*40);}}

TryItOnline

\$\endgroup\$
1
\$\begingroup\$

Ruby, 78 Bytes

loop{print"\e[#{rand(41)};#{rand(41)}H\e[#{rand(30..37)};#{rand(1..3)}m█\e[0m"}

If you count the bytes you'll see that it's more than 78 bytes. That's because I had to replace the ASCII escape character (decimal value 27) with "\e" as stackexchange sanitizes the post. Both can be used in ruby code so the above code will work but for the purpose of golfing you can replace them with the literal escape character.

The code prints a box into the terminal at a random position from 0;0 to 40;40 with a random color.

The result will look something like this:

output on gnome terminal with the solarized color scheme

(result may vary depending on terminal and color scheme)

Explanation

loop {                      # Loop forever
  print "                   # Prints one long string to the terminal     
    \e[                     # Start of an ANSI escape sequence. In this case it
                            # will move the cursor
    #{rand(41)};            # Random y position
      #{rand(41)}H          # Random x position
        \e[                 # Specify the color of the following character
          #{rand(30..37)};  # Set random hue
            #{rand(1..3)}m  # Set random brightness
                    █       # The only thing that will be visible in the terminal
                    \e[0m"} # Reset the color code to keep the blocks from
                            # interfering with each other
\$\endgroup\$
1
\$\begingroup\$

PHP, 194 bytes

My previous answer did not animate; this one does.

<?session_register("i");$i||imagecreatetruecolor(64,64);imagesetpixel($i,rand(64)%,rand()%64,rand()%2**24);header("Content-Type: image/png");header("Location: ".basename(__FILE__));imagepng($i);

save to file, call in browser. It´s as fast as your browser can refresh.
Probably not able to set 4096 pixels in 180 seconds though.

breakdown

<?                                                  # start PHP
session_register("i");                              # register image in session
$i||imagecreatetruecolor(64,64);                    # if no image exists, create one
imagesetpixel($i,rand(64)%,rand()%64,rand()%2**24); # set a random pixel to random color
header("Content-Type: image/png");                  # send HTTP header for image 
header("Location: ".basename(__FILE__));            # send HTTP header to reload
imagepng($i);                                       # send image data
\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 43 41 40 bytes

GPSET RND(40),RND(40),-1-RND(1<<24)EXEC.

I use -1-RND(1<<24) to generate the random color. RND(1<<24) generates a number from 0x000000 to 0xFFFFFF, and -1 - n (equivalent to bitwise NOT) will set the alpha to 0xFF (and flip the bits of the color data but that won't affect the randomness).

\$\endgroup\$
2
  • \$\begingroup\$ The console will still show over the text by default; I'd add a CLS in there to make the answer more friendly. \$\endgroup\$
    – snail_
    May 1, 2018 at 3:00
  • 1
    \$\begingroup\$ ? would also work, because it'll scroll all the text offscreen after 29 iterations. I think I could add it for 1 or 2 characters but I don't think it's necessary. \$\endgroup\$
    – 12Me21
    Feb 7, 2019 at 21:42
1
\$\begingroup\$

Racket (ISL w/ lambda + universe.rkt), 168 bytes

(define(r x)(random x))(big-bang(empty-scene 40 40)[to-draw(lambda(x)x)][on-tick(lambda(x)(place-image(square 1'solid(make-color(r 255)(r 255)(r 255)))(r 40)(r 40)x))])

Ungolfed:

(define (r x) (random x))
(big-bang (empty-scene 40 40)
  [to-draw (lambda (x) x)]
  [on-tick (lambda (x) (place-image (square 1 "solid" (make-color (r 255) (r 255) (r 255))) (r 40) (r 40) x))])

Pretty simple, just places a randomly-colored 1-pixel square at a random location from 0 to 40.

Output after ~3 minutes:

program output

Can anyone guess what book I've been reading, given my language choice?

\$\endgroup\$
1
\$\begingroup\$

Batch 165 156 bytes

  • \E must be replaced with escape character OR download original from pastebin
  • requires windows 10 with virtual terminal sequences enabled
  • note: code contains trailing whitespace
@mode 40,40&@Setlocal EnableDelayedExpansion&@For /l %%i in ()Do @Set/AY=!random!%%40+1,X=!random!%%40+1,c=!random!%%256+1&<nul Set/p=\E[!Y!;!X!H\E[48;5;!c!m 

Pixels

\$\endgroup\$
1
\$\begingroup\$

Java, 401 383 373 bytes

Golfed version:

import java.awt.image.*;import java.util.*;class R{public static void main(String[]a){new javax.swing.JFrame(){int w=200;BufferedImage i=new BufferedImage(w,w,1);Random r=new Random();{setSize(w,w);show();new Thread(()->{for(;;){i.setRGB(r.nextInt(w),r.nextInt(w),r.nextInt(1<<24));repaint();}}).start();}public void paint(java.awt.Graphics g){g.drawImage(i,0,0,null);}};}}

Ungolfed version:

import java.awt.image.*;
import java.util.*;

class R {
    public static void main(String[] a) {
        new javax.swing.JFrame() {
            int w = 200;                           // a constant for width and height
            BufferedImage i = new BufferedImage(
                                        w, w,      // image width and height
                                        1);        // image type BufferedImage.TYPE_INT_RGB
            Random r = new Random();
            {
                setSize(w, w);                     // frame big enough for image
                show();                            // make the frame visible
                new Thread(() -> {                 // create a forever running thread
                    for(;;) {
                        i.setRGB(
                             r.nextInt(w),         // random x position in range 0 .. w-1
                             r.nextInt(w),         // random y position in range 0 .. w-1
                             r.nextInt(1 << 24));  // random RGB value in range 0x000000 .. 0xFFFFFF
                        repaint();                 // trigger a call to the paint method
                    }
                }).start();                        // start the thread
            }

            public void paint(java.awt.Graphics g) {
                g.drawImage(i, 0, 0, null);        // draw the whole image at x=0, y=0
            }
        };
    }
}

Result:

enter image description here

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

Python, 162 bytes

Uses OpenCV library to show the RGB array. Pair of bytes can be saved if define smaller array or a square array.
Note: imshow and waitkey methods are from cv2 module; zeros and random.randint are from numpy module.

from cv2 import *
from numpy import *
h=200
w=200
c=zeros((h,w,3),"u1")
R=random.randint
while 1:
    c[R(h),R(w)]=R(256,size=3)
    imshow("",c)
    waitKey(1)
\$\endgroup\$
0
\$\begingroup\$

Python 3 with Pygame, 171 Bytes

import pygame as p,random as r
r=r.randint
a=p.PixelArray(p.display.set_mode((40,40)))
while(1):
    a[r(0,39),r(0,39)]=(r(0,255),r(0,255),r(0,255))
    p.display.update()
\$\endgroup\$
0
\$\begingroup\$

bash and utils, 45 bytes

Inspired (well, stolen from?) @marcosm answer, here is a quite golfed down version (from his 56 bytes down to 45, shaving the -n 1, and xarg-ing into a printf %s):

shuf -i 0-16777215|xargs printf "\e[48;5;%sm "  # \e is replaced by 1 character, Escape
\$\endgroup\$
0
\$\begingroup\$

Tcl/Tk, 139

Must be run in the interactive shell

gri [can .c -w 40 -he 40]
set x 0
wh 1 {.c cr o $x [set y [exp int(rand()*40)]] $x [set x $y] -f #[form %06x [exp int(rand()*255**3)]]
upd}

Unfortunately, converting expr int(rand()* into a proc makes the script have one byte more!

enter image description here

To stop, one just needs to click the ineffable "X" button.

I've reused the code of present answer on https://codegolf.stackexchange.com/a/148124/29325

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

PostScript, 151 bytes

realtime srand[/PageSize[640 640]>>setpagedevice
10 10 scale([){rand 16#7fffffff div}def(]){rand 64 mod}def{[[[setrgbcolor]]1 1 rectfill flushpage}loop

Needs ghostscript for flushpage; not very fast! Maybe 100 pixels/second on my machine.

screenshot

\$\endgroup\$
1
2

Your Answer

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

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