It's a rainy day

42

13

Context

It's Valentines Day. The only one you ever loved left you yesterday for this guy she always found "stupid and uninteresting". On your way home, you've been stuck in traffic, listening to old songs on the radio, the rain hitting the windscreen is rocking you. After a while in your car, you find yourself alone in your small apartment being unable to think of something else but her. There is no light and you stare trough the window, letting darkness surrounds you. There is no one to talk to, your friends are now gone a long time ago after warning you about this new girl haunting your mind. You start up your computer, as it's the only thing you can do, open your browser and post a new programming puzzle to stackexchange, in an attempt to change your mind.

Challenge

Write a program in the language of your choice simulating the rain which falls on the ground. The output can be composed of ASCII characters or 2D/3D rendered. The camera is fixed : you are above looking straight to the ground. Your program must includes some kind of animation like refreshing the console or the page each time you generate a new "frame". It has to be realistic, I know it's a bit subjective but let's say you can't just fill all the ground in only one big drop.

The output don't have to be an image but if you're using cryptic language it's better to provide a .gif to illustrate how it looks in action (but of course, it will not affect your score if you don't).

Rules

  • Your score is the total of bytes used
  • -20 points if you use colours
  • -50 if you achieve to render some kind of wind
  • Lowest score wins

Example

A very basic example of what should be rendered:

I hope you'll do better and enjoy this challenge.

Sorry for my english, feel free to edit my post if you want to correct some errors

Leader board

If your name is not listed it's because your attempt has been considered not fitting the rules.

Tobia - APL - 35  
j6m8 - Processing.js - 38
The Guy with The Hat - Processing - 42  
ace - Processing - 74  
kelunik - JS/CSS - 89  
Riot - Bash - 91  
Michael - JS/jQuery - 105  
Florent - HTML/JS - 123  
David Carraher - Mathematica - 134  
Doorknob - HTML/JS - 150  
undergroundmonorail - Python - 175

Congratulations to Tobia !

user16229

Posted 2014-02-13T20:33:57.007

Reputation:

Can I reuse my own answer https://codegolf.stackexchange.com/a/143018/29325 ?

– sergiol – 2017-11-11T00:17:47.997

51I sincerely hope the first paragraph isn't a true story. – Kendall Frey – 2014-02-13T20:53:01.003

@KendallFrey Indeed. – cjfaure – 2014-02-13T21:01:42.240

"you are above looking straight to the ground."?? – DavidC – 2014-02-13T22:20:46.953

1@DavidCarraher yes, like you're in the sky looking at the floor. – None – 2014-02-14T10:12:53.967

1How does wind affect what rain falling on the ground looks like? IMO no-one would be able to get those 50 points as the requirement doesn't make sense. But I don't spend a lot of time looking at the ground outside in the rain, so perhaps I'm missing something. – Dukeling – 2014-02-14T10:57:32.200

@Dukeling, If you manage to add scaling on drops, you can easily add a wind effect. – None – 2014-02-14T11:04:00.327

@BenH Assuming that the patch of ground you are looking for, is surrounded by other ground, then the wind that would blow rain to the right, would also blow wind from off frame, into frame. – Cruncher – 2014-02-14T13:54:12.757

8I don't think this should be code-golf as there's too much leniency on the potential outputs – Cruncher – 2014-02-14T13:56:23.127

4The first paragraph along with the camera position for this rendering is quite macabre. – Tobia – 2014-02-14T19:09:18.787

3"stupid and uninteresting" – Professor Allman – 2014-02-14T21:52:28.813

Imagemagick sounds horrifying. I tried to fix that gif in GIMP, but there were 115 frames, so I gave up D: – Pharap – 2014-02-16T01:23:48.423

@BenH I've updated your image; your frames were out of order. Most likely because you exported them as img1, img2, img3 ... instead of img001, img002, img003 ... – primo – 2014-02-18T06:23:26.620

@primo oh thank you, I did not think of that. – None – 2014-02-18T08:31:28.220

Answers

26

APL, 105 chars/bytes* – 20 – 50 = 35 score

e←{⍞←∊'␛['⍵}
e¨'36m' '?25l' '2J'
{⍵←3⌊⍵+3×0=?t⍴50
⍵{(⍵c)←⍕¨⍵+1
e⍵';'c'H',' .∘⍟'[⍺]}¨⍳t
∇0⌈⍵-1}0⍴⍨t←24 80

*: Most APL implementations support some form of (legacy) single-byte charset, that maps APL symbols to the upper 128 byte values. Therefore, for the purpose of golfing, a program that only uses ASCII characters and APL symbols can be scored as chars = bytes.

I tested it on Nick's latest apl.js on Node.js in an OS X terminal. But I haven't used anything specific to his dialect, so it should work on any modern APL that can be run on an ANSI terminal and supports d-funs {...}, strand assignment (a b)←..., and commute , such as Dyalog for Linux or for Raspberry PI (with ⎕IO←0)

The in line 1 is a literal escape character (which is 1 byte). You can input it using Ctrl-V Esc in a Linux terminal or in Vim, or supposedly something like Alt-027 in Windows. Also, I couldn't find a reliable way to discover the terminal size, so you might want to edit the number of rows and columns at the end of the last line.

I defend the 50 bonus by the fact that each raindrop goes through the following shapes: ⍟∘. which give the impression of a slight downwards wind, given that the scene is being looked at from above. In fact, looking at the gif below, you should get the impression that each drop is gently moving downwards and to the left, before disappearing on the ground.

Ungolfed version:

e←{⍞←∊"␛["⍵}                  # utility to print escape sequence
e¨'36m' '?25l' '2J'            # set cyan, hide the cursor and clear screen
{                              # repeat (⍵=current board of raindrops)
  ⍵←3⌊⍵+3×0=?t⍴50              #   add some new drops (=3) in random places
  ⍵{                           #   print the drops (⍺=drop value, ⍵=coords)
    (r c)←⍕¨⍵+1                #     convert the coordinates to string
    e r';'c'H',' .∘⍟'[⍺]       #     print or clear the drop
  }¨⍳t                         #   ..
  ∇0⌈⍵-1                       #   remove 1 from every drop and repeat
}0⍴⍨t←24 80                    # ..starting with an empty board

Output:

enter image description here


APL, different style

Out of competition.

m←×/t←1+(ζη)←2×(βγ)←24 80
e←{⍞←∊(⎕UCS 27)'['⍵}
s←{⍵[β-1-⍳β;1+⍳γ]}
p←{⍺{e'H'⍺,⍨{⍺,';',⍵}/⍕¨⍵}¨(,s⍵)/,1+⍳βγ}
e¨'2J' '36m' '?25l'
{'/'p⍵←(200<m÷?t⍴m)∨0⍪⍵[⍳ζ;1+⍳η],0
' 'p(~⍵)∧0,⍵[1+⍳ζ;⍳η]⍪0
'.∘°'[?(+/,sδ)/3]pδ←⍵∧~d←.2<m÷⍨?t⍴m
∇⍵∧d}t⍴0

Here my aim was to give the impression of raindrops falling with a slant and accumulating on the ground, while trying to keep the number of visible drops (either falling or splattered) constant on average. The trick was to create a number of new falling drops / at every cycle and having the falling drops "wipe out" any splattered ones they travel across.

The result is strangely reminiscent of the Matrix code.

Output
(the jerk every 5s is the gif looping)

enter image description here

Tobia

Posted 2014-02-13T20:33:57.007

Reputation: 5 455

This is nice, but I don't think the ESC character is being printed right. http://i.stack.imgur.com/vLERQ.png I tried pasting the code with gedit too, didn't work.

– Riking – 2014-02-15T02:11:26.893

You got your score wrong. The question says "bytes", not "chars". – jazzpi – 2014-02-15T09:37:33.760

1@Riking I edited that part. If you try it again it should work. – Tobia – 2014-02-15T12:44:32.527

@Tobia Show me a picture of you sitting at your IBM 5100, and I'll buy the chars = bytes argument. – primo – 2014-02-15T14:26:08.960

4

@primo http://imageshack.com/a/img811/314/86dt.jpg

– Tobia – 2014-02-15T17:25:02.570

@Tobia I concede the point ;) – primo – 2014-02-15T18:02:32.657

42

Bash: 111 bytes - 20 = 91 points!

A contemplative gentle drizzle in your terminal. Adjust the numbers 819 and 41 for different height and width respectively.

e='printf \e';while :;do for i in {0..819};do((RANDOM<9))&&$e[1\;36m.||$e[1C;((i%41<1))&&$e'
';done;$e[20A;done

Screenshot

A pleasant bonus is the way the cursor pitter-patters across the rain area.

Edit: shortening from 140 bytes to 129 bytes thanks to @manatwork's suggestions. 2nd edit: shortening from 129 bytes to 111 bytes thanks to @manatwork's and @Tobia's suggestions, plus additional inspiration - see comments.

(Note: screenshot shows previous less-golfed version of the code, which is functionally identical)

Riot

Posted 2014-02-13T20:33:57.007

Reputation: 4 639

1You can spare: 2 characters by joining echo's options; 2 characters by using \e instead of \033; 3 characters by using : instead of true; 5 characters by using arithmetic evaluation (((…))): e='echo -ne \e';while :;do for i in {0..19};do for i in {0..40};do ((RANDOM<9))&&$e"[1;36m".||$e[1C;done;$e' ';done;$e[20A;done. – manatwork – 2014-02-14T09:46:03.170

@manatwork: all excellent suggestions, can't believe i missed combining echo's options! Thanks! – Riot – 2014-02-14T16:19:46.743

1You not need the sigil inside arithmetic evaluation. And there must be a trailing newline at the end of your code. Should be only 127 characters. – manatwork – 2014-02-14T17:46:11.713

1I believe what @manatwork means is that you don't need to use the dollar sign inside double parentheses: ((RANDOM<9)) works just as well. Also, you could try shrinking your code by combining the two for {0..19} and {0..40} into a single for {0..819}, using something like $((i%41)) inside it. – Tobia – 2014-02-14T19:24:14.523

Not to mention, this [1\;36m is one char less than this "[1;36m" and you can probably take out the space before ((. – Tobia – 2014-02-14T19:28:09.023

1Someone stop me!! e='printf \e' is 2 char shorter than e='echo -ne \e'! – Tobia – 2014-02-14T19:34:13.143

Hide that cursor with \e[?25l – Michael Hampton – 2014-02-14T19:42:43.097

1And trim another char by using $e[C – Tobia – 2014-02-14T19:47:07.040

@Tobia - thanks for the suggestion, it wasn't quite that simple but i used ((i%41<1))&& instead. $e[C doesn't work correctly for me... Michael - i'd hide the cursor but it would a) lower my score and b) look less pretty :) – Riot – 2014-02-14T22:06:02.070

After running this, your terminal will be set to bold blue text. Fix that by running $e[0m. – tbodt – 2014-02-17T01:06:16.647

Or just reset. That only happens if your prompt has no colour control characters in it. – Riot – 2014-02-17T04:46:58.957

39

Python, 312 bytes - 50 (wind) = 262

from pygame import*
R=__import__('random').randint
t,u=640,480;init();d=display;s=d.set_mode((t,u))
w=[255]*3;r=range(t)
a=[[R(0,t),R(0,u),R(3,6)]for i in r]
while time.wait(9):
 d.flip();event.get();s.fill(R(0,99)<1and w)
 for i in r:x,y,z=a[i];draw.line(s,w,(x,y),(x+z,y+2*z));a[i][0]=(x+z)%t;a[i][1]=(y+z*2)%u

Sample output (a 50-frame loop):

Actual playpack is significantly faster than gifs allow.

primo

Posted 2014-02-13T20:33:57.007

Reputation: 30 891

You could do w=(0,0,255) instead of w=[255]*3 and get -20 points – Serial – 2014-02-14T07:50:07.523

3@ChristianCareaga I could, but I think white looks better. – primo – 2014-02-14T07:53:04.440

8Only problem in my eyes: You're viewing the rain from the side, while the rules state a fixed camera looking straight down. – Johannes H. – 2014-02-14T09:13:49.140

7@JohannesH. Or, it's a very, very windy day. – primo – 2014-02-14T09:16:55.510

@primo: Given the long distance but slow speed, it's more like slightly windy but a very, very hight tower you're at :D – Johannes H. – 2014-02-14T09:19:25.900

Oh, plus a camera with very bad FPS and long duration of exposure, as each drop is quite long (but moving to slow for strong wind). I think we got that covered now ^^ – Johannes H. – 2014-02-14T09:21:42.020

1@JohannesH. "Actual playpack is significantly faster than gifs allow." By significant, I mean approximately 7 times faster. – primo – 2014-02-14T09:22:58.680

1Nice one, but it's a side view. It's precised in the description that your program must simulates rain hitting the ground. – None – 2014-02-14T10:13:59.237

@primo General way to fix this is remove all but every 7th frame, and use a 7x longer sample – Cruncher – 2014-02-14T13:57:39.897

20I like the occasional flashes of lightning! :P – The Guy with The Hat – 2014-02-14T16:56:04.387

1-1: Not an answer to this question – user12205 – 2014-02-14T16:59:33.403

@ace I claim that this is a top view, with very high wind, and that the droplets are indeed falling towards the ground. I fail to see how this doesn't meet the specification. – primo – 2014-02-15T00:25:45.473

@primo maybe because the droplets never reach ground, and no height difference can be perceived? – user12205 – 2014-02-15T00:43:40.910

@ace there are in fact four different depths. If you don't like the submission, you don't need to upvote it. – primo – 2014-02-15T00:57:05.703

3One character gets saved by using R=__import__("random").randint instead of the from random... line. – SimonT – 2014-02-15T06:29:05.690

35

HTML / JS, 170 chars - 20 = 150 points

<canvas id=c></canvas><script>d=400;with(c)width=height=d,t=getContext('2d');t.fillStyle='blue';setInterval("t.fillRect(Math.random()*d,Math.random()*d,5,5)",50)</script>

(sidenote: golfed further by passing a string to setInterval, with, automatic ID variable names... it feels so wrong! shudders)

It just draws random blue rectangles.

HTML / JS, 309 chars - 20 - 50 = 239 points

Now with wind!

<canvas id=c></canvas><script>s=400;r=Math.random;with(c)width=height=s,t=getContext('2d');t.fillStyle='blue';o=[];setInterval("t.clearRect(0,0,s,s);for(i=0;++i<o.length;)d=o[i],t.fillRect(d[0],d[1],d[2],d[2]),d[0]+=1,d[1]+=2,d[2]-=1,d[2]<0?o.splice(i,1):0;if(r()<.6)o.push([r()*400,r()*400,20])",50)</script>

Doorknob

Posted 2014-02-13T20:33:57.007

Reputation: 68 138

163 bytes: <canvas id=c /><script>d=400;with(c)width=height=d,t=getContext('2d');t.fillStyle='blue';setInterval("t.fillRect(Math.random()*d,Math.random()*d,5,5)",50)</script> AAAHHH! I used with! I feel dirtier than passing a string to setInterval :p – Niet the Dark Absol – 2014-02-13T23:23:02.943

@NiettheDarkAbsol Thanks ;) It seems that (on Chrome at least) canvas can't auto-close, but other than that that works perfectly! (Also, using automatic ID variable names feels so dirty as well :D) – Doorknob – 2014-02-13T23:29:19.413

Some browsers seem more lenient in that area... But it avoids having to use onload. I must say I like @Florent's way of avoiding the duplicate Math.random() – Niet the Dark Absol – 2014-02-13T23:30:22.977

@Doorknob canvas autoclose in Chrome! My answer was developed/tested in this browser. – Florent – 2014-02-14T07:57:01.560

I'm not sure I get the wind version – Cruncher – 2014-02-14T13:59:47.453

If they were round instead of square and you managed to create a splash after each one disappears (i.e. another circle that grows and shrinks rapidly) the second one would be amazing. – Dom – 2014-02-14T21:05:05.063

Trimming those bytes.... 301-20-50=231 points! <canvas id=c /><script>s=400;r=Math.random;with(c)width=height=s,t=getContext('2d'),x=40;t.fillStyle='#008';o=[];setInterval("t.clearRect(0,0,s,s);for(i=0;++i<o.length;)d=o[i],t.fillRect(d[0],d[1],d[2],d[2]),d[0]+=1,d[1]+=2,d[2]-=1,d[2]<0?o.splice(i,1):0;if(r()<.6)o.push([r()s,r()s,x])",x)</script> – Matt – 2014-02-15T19:18:56.820

1Upvote for the second one, with the drips sinking into the ground/puddle. – GreenAsJade – 2014-02-16T07:58:08.110

@Doorknob Save 6 bytes: r=Math.random;t.fillRect(r()*d,r()*d,5,5) – Florent – 2014-02-16T10:28:27.680

34

JS + jQuery (172-20-50 = 102)

Copy/Paste that line in the browser console (generally press F12 key) :

r=Math.random;w=$(window);setInterval("$('<b>♥</b>').css({color:'red',position:'fixed',top:r()*w.height(),left:r()*w.width()}).appendTo('body').animate({fontSize:0},3e3)",9)

Animated red hearts rain for Valentine's day !

enter image description here

Michael M.

Posted 2014-02-13T20:33:57.007

Reputation: 12 173

I'm on firefox right now and when I paste your code in the console, there is a single hearth falling at the speed of light and that's all. Also don't forget that the rain must hit the ground and not just fall down. – None – 2014-02-14T10:15:47.983

@BenH, should work now... at the cost of 3 more bytes. – Michael M. – 2014-02-14T10:22:40.687

it works now, thanks for the correction. But does not qualify until you swap your point of view. Also I can't really see the wind effect because hearts are falling too fast. – None – 2014-02-14T10:25:58.997

Ohhh missed the point "you are above looking straight to the ground" ! – Michael M. – 2014-02-14T10:30:17.183

@BenH, does it satisfy the conditions now ? – Michael M. – 2014-02-14T10:34:40.727

1well, it's not realistic. Hearts are coming from the top of the screen and stick to random positions of the page. That does not really match the point of view condition. – None – 2014-02-14T10:39:55.670

Well, a hearts rain is not realistic ! :) Anyway, I've changed the animation, let me now if it's better for you. – Michael M. – 2014-02-14T10:47:20.773

I was about to tell you about scaling. It's neat but reversed, if you're in the sky and looking to the ground, hearts shrink as they fall. :) I think you should also slow down the animation a bit and work on the wind effect because for now I can't see it. You're in the right way ! – None – 2014-02-14T10:51:43.317

This is very similar to effect used on this site: 2-ch.so/b/ (appears only today) – Display Name – 2014-02-14T14:17:43.280

10AUGGGGHH! How do I stop it?! – The Guy with The Hat – 2014-02-14T15:54:05.800

@TheGuywithTheHat Just close the tab. – None – 2014-02-14T16:52:19.000

1@user2509848 But is there any other way? – The Guy with The Hat – 2014-02-14T16:54:48.757

1Also it should be byte count, so in case you're using UTF-8 the heart counts as three bytes. – SztupY – 2014-02-14T17:05:39.423

@TheGuywithTheHat: Check out the answer to http://stackoverflow.com/questions/6843201/how-to-clearinterval-with-unknown-id.

– Peter Majeed – 2014-02-14T17:43:40.110

3@TheGuywithTheHat, reload the page – Michael M. – 2014-02-14T18:42:09.960

19Brilliant! I think this captures the heart of the question. – andrewb – 2014-02-14T23:17:43.723

1@andrewb that was punny – Joe the Person – 2014-02-16T04:29:30.070

26

Mathematica

134 - 20 = 114

2D

n = 99; m = Array[0 &, {n, n}]; r := RandomInteger[{1, n}, {2}]
Table[ArrayPlot[m = ReplacePart[m, r ->  1], ColorRules -> {1 -> Blue}], {k, 250}];
Export["d.gif", d]

2D


3D

The raindrop shape is made via a revolution plot around the z axis.

Initially, rain is generated for a region that extends well above the display region. The appearance of falling rain is achieved by shifting the Viewpoint upwards along the z-axis. (It is more efficient than recalculating the position of each raindrop.)

rain

r = RandomInteger; z = Table[{r@30, r@30, r@160}, {100}];
w = RevolutionPlot3D[{.7 Sin[x] Cos[x], 0,   1.4 Sin[x] }, {x, 0, -Pi/2}, 
PerformanceGoal -> "Speed"][[1]];
c = Map[Translate[w, #] &, z]; 
p = Table[Graphics3D[c, PlotRange -> {k, k + 50}], {k, 1, 100}]
Export["p.gif", p]

With Wind

There is considerable overhead to making the rain fall with wind. But I'm including here anyway.

The blue floor pretty much keeps {x,y} view region confined to the area of interest. There are some glitches, but, oh well,

r = RandomInteger;
z = Table[{r@120, r@30, r@180}, {800}];
w = RevolutionPlot3D[{.7 Sin[x] Cos[x], 0,   1.4 Sin[x] }, {x, 
     0, -Pi/2}, PerformanceGoal -> "Speed"][[1]];
c = Map[Translate[w, #] &, z];
g[k_, z1_, w_, c1_] :=
 Module[{z2},
  z2 = Cases[z, {x_, _, _} /; 0 + k < x < 30 + k];
  c = Map[Translate[w, #] &, z2];
  Graphics3D[{Polygon[{{0 + k, 0, 1 + k}, {30 + k, 0, 1 + k}, {30 + k,
        30, 1 + k}, {0 + k, 30, 1 + k}}], c}, 
   PlotRange -> {k, k + 50}]]

p = Table[g[k, z, w, c], {k, 1, 100, 1}];
Export["p.gif", p]

with wind


From Directly Above

The closest raindrops are clipped but I'll overlook that.

from above 3D

m=40;
r=RandomInteger;
positions=Table[{r@m,r@m,r@1000},{800}];
g[lowZ_,pos_]:=
Module[{hiZ=lowZ+103},
Graphics3D[{PointSize[Small],White,Point[{{0,0,lowZ},{0,m,lowZ},{m,0,lowZ},{m,m,lowZ},{0,0,hiZ},{0,m,hiZ},{m,0,hiZ},{m,m,hiZ}}],
ImageSize-> 350,Sphere/@Cases[pos,{_,_,z1_}/;lowZ<z1<hiZ-2]},PlotRange->{lowZ,hiZ}, 
ViewPoint-> {0,0,1},ImagePadding->5]]

DavidC

Posted 2014-02-13T20:33:57.007

Reputation: 24 524

Shift it to the side in under 50 characters and you lower your score. :) – Ali Caglayan – 2014-02-14T18:47:53.077

1I love this one – None – 2014-02-15T16:37:32.317

+1 for the last one, with the correct camera angle BUT you forgot that the camera blocks some of the rain.... ;) – GreenAsJade – 2014-02-16T07:59:11.617

@GreenAsJade, The clipping for the 3D (from above) viewpoint is now fixed. – DavidC – 2014-02-17T16:54:08.053

Sweet, but I was being more frivolous than you thought. I was meaning that there is a physical camera looking down at this, so it should be blocking some of the raindrops :%) – GreenAsJade – 2014-02-18T00:06:51.783

10

HTML / JavaScript, 156 123 (143 - 20)

<body bgcolor=0 onload="t=c.getContext('2d');t.fillStyle='#07d';setInterval('n=Math.random()*4e4;t.fillRect(n%270,n/150,1,1)',1)"><canvas id=c>

Annotated version:

<body bgcolor="#000">
<canvas id="c"></canvas>
<script>
  onload = function() {
    // Retrieve the rendering context
    t=c.getContext('2d');
    // Set rain color
    t.fillStyle='#07d';
    // Render whenever it is possible
    setInterval(function() {
      // Generate a number between 0 and 40,000
      // 40,000 ~= 270 * 150
      n=Math.random()*4e4;
      // Draw a raindrop.
      // Since x and y are not rounded, the raindrop looks blurry!
      t.fillRect(n%270,n/150,1,1)
    }, 1);
  };
</script>
</body>

Florent

Posted 2014-02-13T20:33:57.007

Reputation: 2 557

7

Smalltalk (Smalltalk/X)

with random wind ;-)

|BG CLR N1 H W v WIND drops gen newDrops draw remove move buffer|


BG := Color black.
CLR := Color blue lightened.
H := 100.
W := 100.
N1 := 10.
WIND := 0.
drops := OrderedCollection new.

gen := [:n | ((1 to:n) collect:[:i | Random nextIntegerBetween:1 and:W] as:Set) collect:[:x | x@0]].
newDrops := [drops addAll:(gen value:N1)].
draw := [buffer fill:BG; paint:CLR. drops do:[:d | buffer displayPoint:d]].
remove := [drops := drops reject:[:d | d y > H]].
move := [:wind | drops := drops collect:[:d| (d x + wind)\\W @ (d y + 1)]].
v := View new openAndWait.
buffer := Form extent:(v extent) depth:24 onDevice:v device.

[
    [v shown] whileTrue:[
        draw value.
        v displayForm:buffer.
        move value:WIND.
        remove value.
        newDrops value.
        WIND := (WIND+(Random nextBetween:-1 and:1)) clampBetween:-5 and:5.
        Delay waitForSeconds:0.1.
    ]
] fork.

output in view: enter image description here

blabla999

Posted 2014-02-13T20:33:57.007

Reputation: 1 869

6It does not qualify since it's a side view of the rain falling, and not the rain hitting the ground. Still a nice wind effect. – None – 2014-02-14T10:11:35.863

6

Processing, 94 - 20 = 74

void setup(){background(0);fill(0,0,255);}
void draw(){ellipse(random(0,99),random(0,99),3,3);}

(New line added for readability.)

Click here for an online demo.

user12205

Posted 2014-02-13T20:33:57.007

Reputation: 8 752

1Processing is really good for problems like these. – cjfaure – 2014-02-13T21:01:59.303

Cannot view it in Firefox, default security settings block the Java applet and it doesn't tell me how to override. AFAIK there is a Processing.js port to JavaScript, if you could convert it to JS and post a link I'd be thankful. – marczellm – 2014-02-14T17:06:43.587

@marczellm I'm using Firefox 27.0 on Ubuntu and it works fine. Are you sure there are no prompts asking you whether to allow or block the plugin? I will take a look at a JS version as well. – user12205 – 2014-02-14T17:11:11.760

@ace After I tell all promps to "allow", a security message still blocks it. Thanks for the JS version. – marczellm – 2014-02-14T17:21:22.863

1@marczellm added the javascript version into the answer, enjoy :) and just a blind guess, maybe your current java plugin version is outdated and has known security issues – user12205 – 2014-02-14T17:21:24.913

5

Bash

while true;do echo " / / / / /";echo "/ / / / / ";done

I'm not sure this should be a code golf because there isn't a strict requirement on what the "rain" must look like.

EDIT: If you want it to look like the camera is pointing straight down use this:

while true;do echo " . . . . .";echo ". . . . . ";done

user16402

Posted 2014-02-13T20:33:57.007

Reputation:

7"The camera is fixed : you are above looking straight to the ground." This appears to be viewing the rain from the side? – undergroundmonorail – 2014-02-13T21:25:18.230

2Save 9 characters: while echo \ / / / / /;do echo / / / / /;done (or a few more with a recursive function but that'll quickly blow up the stack). @undergroundmonorail Strong wind, diagonal relative to the camera. – Gilles 'SO- stop being evil' – 2014-02-14T04:27:48.913

@Gilles In that case, we can do while echo -e '\e[0;34m / / / / /';do echo / / / / /;done and get it down to -13 with bonuses :) – undergroundmonorail – 2014-02-14T05:19:59.283

4that's defintely not looking like rain :D – Kiwy – 2014-02-14T09:14:18.163

@Kiwy true that :D – None – 2014-02-14T10:23:44.790

5I don't really think this should be considered as an answer. In short: It's not realistic. Detailed argument: If I interpreted the question correctly, the rain should fall randomly (or at least pseudo-randomly) and, over a long period of time, the distribution should be similar per unit area. (This is how rain works, right?) However in this answer, it is guaranteed that there will be no rain drops next to any rain drop, so if we take the unit area to be the area of one character, we see that the distribution is uneven. – user12205 – 2014-02-14T17:07:38.367

@ace you're probably right... although the question said nothing about randomness – None – 2014-02-15T16:25:12.160

@professorfish the question did mention "It has to be realistic" – user12205 – 2014-02-15T19:42:28.617

@ace fine, I give up... – None – 2014-02-16T07:14:22.407

3

Python 2.7: 195 - 20 = 175

I'm sure there's more that can be done here, but this is what I've got for now:

import os,time
from random import*
l=[i[:]for i in[[' ']*100]*50]
while 1:
 os.system('clear')
 l[randint(0,49)][randint(0,99)]='.'
 print'\033[94m\n'.join(''.join(r)for r in l)
 time.sleep(.05)

I'll post a gif of the output when I remember how to do that.

This works on linux. Replacing 'clear' with 'cls' makes it work on windows, but then ANSI colours don't work and I lose the bonus.

I have a 2D array of one-character strings, initialized to . Every 0.05 seconds, one of them is chosen at random set to ., and the screen is redrawn.

from random import* saves two characters over import os,time,random and using random.randint() twice, though I'm not convinced that's the best way to choose a cell anyway. I wanted to use random.choice() but I couldn't think of a way around immutable strings that wouldn't waste more characters than it saved.

undergroundmonorail

Posted 2014-02-13T20:33:57.007

Reputation: 5 897

3l=[i[:]for i in[[' ']*100]*50], as per http://stackoverflow.com/a/6688361/1114687. 198 - 20 = 178 – n.st – 2014-02-14T02:10:32.570

Oh, wow, I've never seen that before. If I'm reading it correctly, the slice notation does nothing except ensure that it's a unique list and not another reference to the same one, correct? That's really cool! Thank you! – undergroundmonorail – 2014-02-14T05:09:01.830

I originally tried l=[[' ']*100]*50, but that just creates 50 references to the same 100-element list, so I searched for the shortest possible way to circumvent that and found the Stack Overflow answer linked above. – n.st – 2014-02-14T18:10:45.760

2

132 + 27 - 20 - 50 = 89

Javascript (132)

r=Math.random;setInterval("$('body').append($('<i>∘</i>').css({left:r()*2e3,top:r()*2e3}).animate({left:'+=70',fontSize:0},500))",1)

CSS (27)

i{color:blue;position:fixed

Demo: http://jsfiddle.net/kelunik/5WC87/4/embedded/result/

kelunik

Posted 2014-02-13T20:33:57.007

Reputation: 160

It's from side view, it does not qualify for the moment. – None – 2014-02-14T16:53:30.847

@BenH You're right, missed that point, new version is in my answer now. – kelunik – 2014-02-14T17:07:36.347

seems like nothing happens when trying your code in firefox console :/ – None – 2014-02-14T17:49:13.867

@BenH That's why there's a jsfiddle-demo. – kelunik – 2014-02-14T17:54:22.233

didn't see it. thanks – None – 2014-02-14T17:55:13.167

1

Tcl/Tk, 139 - 20 = 119

Reusing my own answer http://codegolf.stackexchange.com/a/143018/29325

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.

sergiol

Posted 2014-02-13T20:33:57.007

Reputation: 3 055

1

Processing, 62 - 20 = 42

void draw(){stroke(0,0,214);point(random(0,99),random(0,99));}

Generates blue pixels on a white background. Demonstration in a very similar language here: https://www.khanacademy.org/cs/rain2/6172053633761280

The Guy with The Hat

Posted 2014-02-13T20:33:57.007

Reputation: 778

Obligatory +1 for extreme terseness. – primo – 2014-02-17T13:54:28.230

1

Processing.js, 86 - 20 = 66

...but it also slowly fades out (the ground absorbs the rain, naturally). Points for that?

g=99;r=random;void draw(){fill(0,9);rect(0,0,g,g);fill(0,g,r(g));rect(r(g),r(g),2,2);}

Bonus features include varying between greenish and blueish (it's clearly dirty 'city' rain).

Also, I was very pleased that I got to use a JavaScript hack in here; Note that, because this is processing.js, you can throw in things like the typeless declaration of g=99, or the alias of r for random (cross-language alias!).

Any other ideas to minify?

Readable version:

g = 99;
r = random;                  // Javascript trickery
void draw() {
    fill(0, 9);
    rect(0, 0, g, g);        // Fade the background
    fill(0, r(g), r);
    rect(r(g), r(g), 2, 2);  // Add a new drop
}

The whole thing can be viewed here.

...plus another version without fade: 58 - 20 = 38

If you don't like fading and don't mind grey dirt:

r=random;void draw(){fill(0,0,255);rect(r(99),r(99),2,2);}

j6m8

Posted 2014-02-13T20:33:57.007

Reputation: 213