Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Imgur logo

Imgur is a free image hosting service. Many people use it. Here is an example of an imgur link: Write a program that continually outputs random (valid) imgur links. For example, here is some sample output of my progam (not shown because it contains some tricks you will have to figure out yourself):

Helpful hints:

  • When imgur was new, 5-letter links were used.
  • When imgur was new, numbers weren't used.
  • You can use this to your advantage: only find 5-letter link images with only letters. That is what my program does.
  • Also, all images are saved as .png.


  • Continually output random imgur links
  • Links considered sufficiently "random" if 50 are outputted with no repeats
  • When visited, links must be an image
  • Links must start with and end with .png
  • Score is amount of characters

I did it in Java (TERRIBLE for golfing) in 452 chars. Not shown here because it contains some tricks you will have to figure out for yourself!)

share|improve this question
Wait, is your definition of random that the output shouldn't contain repeats? – Mr Lister Jan 20 '13 at 9:23
Warning: pornographic images may be present on imgur! =:o – Mr.Wizard Jan 20 '13 at 22:52

12 Answers 12

up vote 3 down vote accepted

PHP 5.4, 76 characters

URLs are generated in sequential order using only uppercase letters and never repeat, meeting the letter of the specification.

share|improve this answer
This does not meet the requirements. Many of the URLs it generates are not valid. – Doorknob Jan 20 '13 at 13:11
@Doorknob: Which ones? I checked the first few, and all links were valid. – Thomas W. Jan 20 '13 at 17:43
Doesn't this just print,, etc.? EDIT: oh wait never mind. I just skimmed the code :P Upvote then! – Doorknob Jan 21 '13 at 0:07

HTML (152)

<img src=x

This logs all found images on the JavaScript console using console.log(). Works in all tested browsers (Firefox, Chrome, IE9, Safari and Opera).

The fun part is that all sorts of funny images are flashing up for the blink of an eye :).

Try it! (jsFiddle wraps this into a more complete HTML page, but browsers also accept the single element.)

Props to the amazing random string method by doubletap!

Where can I see the JavaScript console and the logged images?

  • Firefox: Press Control-Shift-K (Command-Option-K on the Mac). Unselect the Net, CSS and JS buttons there, only select the Logging button.
  • Opera: Press Control+Shift+i, select the Console tab.
  • Chrome: Press Control+Shift+i, select the Console tab. On the bottom, select Logs.
  • Safari: Basically like Chrome, but make sure first that Safari's developer tools are activated. Then press Control+Alt+C (on Windows, not sure on the Mac) instead of Control+Shift+i, select the Console tab. On the bottom, select Logs.
  • IE: Press F12, select the console tab.
share|improve this answer
This does not meet the requirements. Many of the images it pops up are not valid. – Doorknob Jan 20 '13 at 13:10
What do you mean by "not valid"? In the console, only images that could be successfully displayed are logged (see my updated description). – Thomas W. Jan 20 '13 at 14:56
Oh, okay. Retrying now. Edit: Chrome seems to be logging the invalid images as well. – Doorknob Jan 20 '13 at 14:57
Chrome by default mixes error messages and logs. At the bottom of the console tab you have All, Errors, Warnings, Logs. If an image could not be loaded, it displays a 404 error with the image URL. If you select Logs, then only the output generated by my code should display. – Thomas W. Jan 20 '13 at 15:11
A lot of the logged image URLs just return the standard imgur error image that says "The image you are requesting does not exist or is no longer available." I suppose one could argue that those are still valid results under the rules as given, since the returned document is an image. – Ilmari Karonen Jan 20 '13 at 23:22

Perl (87)

perl -pe's/\W//g;$_="$_.png\n";$_=""if`curl $_`=~/^</'</dev/urandom

Finding images with uppercase, lowercase and digits from 0 up to any characters, some day.

share|improve this answer
Hehehe, urandom – beary605 Jan 20 '13 at 14:48

Mathematica, 108

White-space added.

 Import@# /. _Image :> Print@# &[
  "" <> "a" ~CharacterRange~ "z" ~RandomChoice~ 5 <> ".png"
share|improve this answer

Perl (93 + 4 = 97)

Using imgur's own random mechanism to get their image URLs, which aren't png URLs most of the time:

$ perl -Mojo -E 'say+g("")->dom->at("[rel=image_src]")->attrs("href")for+1..50'

(You need Mojolicious for this.)

share|improve this answer
That's brilliant, and so not fair! – elssar Jan 21 '13 at 16:53
@elssar That's right, at least your second point. ;) – memowe Jan 21 '13 at 17:49
Oh, that's clever :) – Doorknob Jan 22 '13 at 0:40

Python (174 158 156)

I want shorter module names in Python. Also an easier method of getting random letters. :)

import urllib,random
while 1:a=''%''.join(chr(random.randint(65,90))for i in'AAAAA');print('File'not in urllib.urlopen(a).read())*a


The modulus operator on a string is the formatting command, in this case it replaces '%s' in the string with 5 random uppercase letters
a is the website name (type str)
('File'not in urllib.urlopen(a).read()) is True when 'File' (from 'File not found!') is not found in the the HTML of the URL. (type bool)
bool * str = str if bool = True, so it will only output a if 'File' is not found in the HTML code.

share|improve this answer
What part of the spec says you have to use both upper and lower case letters? chr(random.randint(65,90)) – Peter Taylor Jan 20 '13 at 8:49
I ran it and it didn't output anything. Nice trick with 'File' though :) – Doorknob Jan 20 '13 at 13:14
@PeterTaylor: :) I didn't know that there were any links on imgur with only uppercase letters, but I guess that would be obvious enough. Thanks! – beary605 Jan 20 '13 at 14:30
@Doorknob: It outputs a newline unless a link is found, then it will give a link. – beary605 Jan 20 '13 at 14:57
Any sequence of length 5 will work for your 5 random characters, so you can shave a few characters off by using something like 'XXXXX' rather than range(5). This also has the advantage of not requiring whitespace between it and the in. – Strigoides Jan 21 '13 at 1:33

Unique Python approach. 361 355 334 332 322 314 characters -- a little obfuscated, nothing too difficult. May result in unusually high density of cat pictures, you have been warned.

import json as j,urllib as o,time as t;a=0
while 1:
 for s in c[p]:
  if w.find(q)!=-1:print w

share|improve this answer
All the images... are animals? There is some trickery going on here :P +1 because I like the cute animals :3 – Doorknob Jan 22 '13 at 0:38

R, 182 characters

library(httr);while(0<1){s=paste("",paste(sample(c(LETTERS,letters),5),collapse=""),".png",sep="");if(HEAD(s)$headers$'content-type'=='text/html')'' else print(s)}
share|improve this answer

Bash (129, 121) (117, 109)

I've got two versions: an iterative and an endless recursive one (which will slowly eat up all memory). Both versions check if there actually is a PNG file present (jpg's, gif's and other file types are ignored).

Iterative(old) (129):

while true;do u=$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -s 2>&1|head -c4|grep PNG$ -q&&echo $u;done

Recursive(old) (121):

:(){ u=$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -s 2>&1|head -c4|grep PNG$ -q&&echo $u;:;};:

Note: There might be a compatability issue with grep. My grep manual states that -s silents grep's output but it does nothing. However, using --quiet, --silent or -q instead works.


Using content headers now after reading :)

Iterative (117):

while true;do u=$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -sI|grep image -q&&echo $u;done

Recursive (109):

:(){ u=$(tr -dc a-zA-Z</dev/urandom|head -c5).png;curl $u -sI|grep image -q&&echo $u;:;};:
share|improve this answer

Ruby (103 chars)

require"open-uri";loop{u="{rand(1e9).to_s(36)[0,5]}.png";open u rescue next;puts u}
share|improve this answer
You have to add a .png to the end of the URL. – Doorknob Jan 22 '13 at 0:36
@Doorknob: Whoops, fixed – anonymous coward Jan 25 '13 at 8:42

Python, 153 chars

import string as s,random as w,requests as r
while 1:
 if'not'not in r.get(t).text:print t

While this works, it is bloody slow and might take many seconds before returning any output.

Inspired by @beary605's solution - he saved me a bunch of characters as I was planning to check for image by content-type header.

share|improve this answer
random.sample :) That is a much better way of choosing characters. – beary605 Jan 21 '13 at 15:38
Yep, amazing what Code golf will force you to Google, and learn. – elssar Jan 21 '13 at 16:51

Bash/command-line tools, 72 chars

Borrowing @memowe's clever technique:

curl -sL|grep e_sr|cut -d\" -f4;exec $0

This achieves a continuous loop by re-execing itself within the same process space.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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