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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Task: Have your program randomly choose one of the following two quotes at runtime and print that one quote, exactly as shown.


Under the spreading chestnut tree
I sold you and you sold me.
There lie they, and here lie we
Under the spreading chestnut tree.



Restrictions: The words spreading, chestnut, PEACE, FREEDOM, and STRENGTH must appear in your code. No word from the above two quotes may appear in your code more than once.

Shortest solution wins.

share|improve this question
Ok I choosed the first. Honest! – edc65 Aug 2 '14 at 8:48
@edc65 That's no longer funny. – Martin Büttner Aug 2 '14 at 9:40
@MartinBüttner not to be funny, but to suggest a more explicit rule. Reading first time I really tought I could choose a random one and then start to work. – edc65 Aug 2 '14 at 10:06
@edc65 Ah I see, the wording used to be different. – Martin Büttner Aug 2 '14 at 10:07

10 Answers 10

up vote 2 down vote accepted

PHP 179

Inspired by es1024's answer.

I 1 232 1 me.
T5 they,35 we
",["Under the spreading chestnut tree",sold,you," and "," IS ","here lie"]);
share|improve this answer

Perl (191 184 characters)

06":"WAR5PEACE6FREEDOM5SLAVERY6IGNORANCE5STRENGTH6";s/\d/("Under the spreading chestnut tree"," sold ",you," and ","here lie "," IS ",".

Thanks @core1024 for tips on golfing this.

share|improve this answer
You can replace 6 with actual new lines, drop the quotes around "you" and use $$&1 as a random generator :) – core1024 Aug 5 '14 at 13:27

Javascript 229 254 256

Edit 1 using new Date as suggested by @Doorknob - still unclear to me how javascript Dates morph to strings or integers or whatever

Edit 2 simplified. A lot.

t=(new Date&1
   :'0Under the spreading chestnut tree0\nI 0sold 0you 0and 04030me.\nT0here lie 0they, 05090we\n010.'
share|improve this answer
new Date is shorter than – Doorknob Aug 2 '14 at 11:13
To explain the Date conversion: > The JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC., when you & it it's automatically cast to a Number. So you're basically making the random choice based on whether it's an even or odd millisecond since the 1970-01-01T00:00:00.000Z epoch. – Bob Aug 2 '14 at 13:47
@Doorknob could you write a short article for that shorter random number generator in… Haven't seen it there yet and I think it could be quite useful. – flawr Aug 2 '14 at 22:23
@flawr Done – Doorknob Aug 2 '14 at 23:21

C# (268)(260) (256)

This is executable in LINQPad, using the Dump() method:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";if(new Random().Next(9)>4)(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".").Dump();else("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.").Dump();


string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";

if(new Random().Next(9)>4)
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".").Dump();


Using the ternary operator and 1 extra variable, I was able to cut another 6 characters:

string a,b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";a=new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.");a.Dump();


string a,b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";

a=new Random().Next(9)>4 ?
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".") :



Thanks to the ingenious suggestion of tsavinho I was able to save 4 more chars by placing braces around the ternary operation:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";(new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.")).Dump();


string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";
(new Random().Next(9)>4?
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):
share|improve this answer
You could save 4 more characters by removing a and replacing it with brackets like this: string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";(new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENG‌​TH.")).Dump(); – tsavinho Aug 6 '14 at 14:51

Python 3 - 228

for x in[b"AB:87078@624>049BA6",b"?;<61;=63;56"][id(id)%3-1]:print("and |FREEDOM|T|IGNORANCE|here lie |STRENGTH|.\n|you |sold |we|I | IS |PEACE|SLAVERY|they, |WAR|me|Under the spreading chestnut tree|\n".split("|")[x-48],end="")

Slightly ungolfed:

# id(id) returns an even number based on memory address of id
# id(id)%3-1 gives -1, 0 or 1
randomNumber = id(id)%3-1

# Word list
words = "and |FREEDOM|T|IGNORANCE|here lie |STRENGTH|.\n|you |sold |we|I | IS |PEACE|SLAVERY|they, |WAR|me|Under the spreading chestnut tree|\n".split("|")

# Byte Literals for Under the chestnut and WAR IS PEACE
# each byte correspond to the word's index in the word list
byteLiterals = [b"AB:87078@624>049BA6", b"?;<61;=63;56"]
choice = byteLiterals[randomNumber]

for x in choice: 
    print(words[x-48], end="") # Print each word out
share|improve this answer

Java, 338

I can't get it below 338...

class B{public static void main(String[]a){String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}}

Bit ungolfed:

class B{public static void main(String[]a){String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";
if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);
else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}}
share|improve this answer
Yes you can…. – Fabinout Aug 5 '14 at 14:29
Thanks for the link! Gonna search for something useful tomorrow. – CoolWilly Aug 5 '14 at 14:40
@Fabinout I've tried a few tricks, but they didn't help. ( p=System.out, added 15 bytes to total, static<T>void p(T o){System.out.print(o);}, added 12). Any other suggestions? – CoolWilly Aug 6 '14 at 7:11
use static block instead of a main declaration – Fabinout Aug 6 '14 at 7:56
Static block *310 CHAR *: class B{static{String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENG‌​TH"+h);}} – Fabinout Aug 6 '14 at 11:36

PowerShell 205

("{0}.`nI{1}{2}{3}{2}{1} me.`nT{4}they,{3} {4}we`n{0}."-f"Under the spreading chestnut tree"," sold"," you"," and","here lie "),("WAR","PEACE.`nFREEDOM","SLAVERY.`nIGNORANCE","STRENGTH."-join" IS ")|Random

Uses the -f operator to put strings on the first quote, and the second one is joined by IS, after that it selects a random element of those 2 quotes and prints it...

share|improve this answer

JavaScript / jQuery 396

var B={
U:"Under the spreading chestnut tree",
s:"sold ",
y:"you ",
l:"lie ",
a:"and ",
i:"IS ",
var A={
P:B.U+B.b+"I "+B.s+B.y+B.a+B.y+B.s+"me."+B.b
+"There "+B.l+"they, "+B.a+"here "+B.l+"we."+B.b+B.U,
Q:"WAR "+B.i+"PEACE."+B.b+"FREEDOM "+B.i+"SLAVERY."+B.b
var z=(Math.random()<0.5)?A.P:A.Q; 
share|improve this answer

T-SQL, 337 327

Just for fun, I've made another solution in T-SQL, which is one byte shorter than my Java 8 solution:

DECLARE @b char(33)='Under the spreading chestnut tree',@c char(5)='sold',@d char(4)='you',@e char(4)='and',@f char(9)='here lie',@ char='
',@h char(4)=' IS'IF rand()>.5PRINT @b+@+'I '+@c+@d+@e+@d+@c+'me.'+@+'T'+@f+'they, '+@e+@f+'we'+@+@b+'.'ELSE PRINT'WAR'+@h+'PEACE.'+@+'FREEDOM'+@h+'SLAVERY.'+@+'IGNORANCE'+@h+'STRENGTH.'

By declaring the chars one too long, you can add an 'automatic' space, saving up a few bytes.

Using the code in the proposed edit by user PenutReaper, you can indeed shave off 10 bytes.

share|improve this answer
Using a variable for carriage returns isn't worth it. Just use literal ones. – Muqo Sep 18 '14 at 23:51

Java Script (233)

Math.random()>0.5?(a="Under the spreading chestnut tree")+"\nI "+(s="sold")+(y=" you ")+(n="and")+y+s+" me.\nT"+(h="here ")+(l="lie ")+"they "+n+" "+h+l+"we\n"+a+".":["WAR","PEACE.\nFREEDOM","SLAVERY.\nIGNORANCE","STRENGTH."].join(" IS ")

Wrote this without looking at other answers. Makes good use of assignments as expressions.

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.