# Duck, duck, goose!

Remember the kids game, 'Duck, Duck, Goose'? No? Me neither.

### The challenge

• Print the word 'duck' on individual lines an indeterminate amount of times.
• Print the word 'goose'.

### The rules

• Attempt to play the game in the fewest bytes.
• There must be at least one duck.
• There must be exactly one goose, at the end of the list.
• There must be exactly one bird on each line. No empty lines.
• The case of the outputted strings is irrelevant.
• White-space within a line is fine.
• Your program must not consistently produce the same number of ducks.

Have fun!

Please note: This question is not a duplicate of Shortest code to produce non-deterministic output

Reasons include:

• The association to a childrens' game
• The defined start and end requirements of the result string. There is no specified output in the other challenge.
• Answers For the other, non-duplicate challenge are in a single-digit number of bytes. The average for this one is around 30, or there about.
• By the amount of overlap between this challenge and that one, any code-golf question including the 'random' tag is a duplicate. Should we delete them all?
• The code answers for this challenge would match the other challenge (in a ridiculously bloated way), but the answers to that challenge would not match this one.
• Could you define indeterminate? Could it mean either zero or one? – recursive Mar 12 '18 at 15:51
• Please define how randomly this should be generated. Uniform in range or with exponential decline? – HyperNeutrino Mar 12 '18 at 15:52
• @recursive Nope, but let's go with a working definition... The program does not consistently present the same number of ducks. – AJFaraday Mar 12 '18 at 15:52
• Speaking as a Minnesotan, what if mine prints "duck, duck, gray duck" instead? :) – Mike Hill Mar 14 '18 at 19:13
• @jpmc26 I’m sure there are others. You’d have had to play it with others, for a start. – AJFaraday Mar 15 '18 at 23:13

## Javascript 796962 60 bytes

-10 bits thanks to @Herman Lauenstein

-7 bits thanks to @binarymax

-2 bits thanks to @Shaggy

_=>{for(_=0;_++<=new Date%9;)(l=console.log)Duck;lgoose}


a=_=>{for(_=0;_++<=new Date%9;)(l=console.log)Duck;lgoose}
a()

• 69 bytes: _=>{for(_=0;_<Math.random()*9;_++)(l=console.log)('Duck');l('goose')} – Herman L Mar 12 '18 at 20:36
• Won't this give you no ducks if random returns zero? – Shaggy Mar 13 '18 at 8:00
• 63 Bytes: _=>{for(_=0;_<=new Date%10;_++)(l=console.log)Duck;lgoose} – binarymax Mar 13 '18 at 15:38
• @binarymax, you can save 2 more bytes on that with _++<=new Date%9 and a few more by using alert instead of console.log. – Shaggy Mar 14 '18 at 17:13
• One byte shorter: for(_=9;_-->new Date%9;). Another four bytes less: for(_=new Date;_--;) (no idea if that works, though) – Titus Mar 14 '18 at 17:27

# Minecraft Functions (18w11a, 1.13 snapshots), 402 382 bytes

Uses three functions in the minecraft namespace

a:

scoreboard objectives add r custom:play_one_minute
scoreboard players set @s t 9
scoreboard players operation @s x = @s r
scoreboard players operation @s x %= @s t
function b


b:

execute if score @s x = @s t run say Goose
execute unless score @s x = @s t run function c


c:

say Duck
scoreboard players add @s x 1
function b


Datapack with the functions

-20 bytes by removing unnecessary namespaces

# Explanation

Function a creates three scoreboard objectives (variables). Objectives with the criteria dummy are like conventional variables, however, r has the criteria custom:play_one_minute. This means it will increase each in game minute. By using modulus it becomes a pseudorandom number between 0 and 9. The t objective is used because there is no way to do math with literal values, so we need a "constant" objective.

The function b is the main loop, calling the c function until the counter objective x is equal to 9 at which point it prints "Goose".

The c function prints "Duck", increments the counter objective and then calls the b function again

# R, 45 bytes

while({cat("duck
");rt(1,1)>0})0;cat('goose')


Try it online!

Outgolfed by @user2390246

Prints a single duck, then with probability 1/2, prints another duck and repeats, or prints goose and terminates.

This could also be without the >0 part, since the Student's t-distribution might eventually return a 0 but that is highly improbable, so I prefer this.

• 35 bytes – user2390246 Mar 12 '18 at 16:38
• @user2390246 I think that's sufficiently different from mine as to warrant its own answer, especially since every answer is bound to be a variant of "generate a random number of ducks one goose"; yours is a quite distinct approach. – Giuseppe Mar 12 '18 at 16:42
• Fair enough, thanks for your sportsmanship! Added as a separate answer – user2390246 Mar 12 '18 at 16:50

# Python 3, 52 bytes

prints 1 to 256 ducks.

import os
print("duck\n"*-~os.urandom(1)[0]+"goose")


Try it online!

# DOS .BAT, 54 Bytes

@for /l %%d in (0,9,%random%)do @echo duck
@echo goose


# 43 Bytes

[main.bat]
@0&echo goose
[0.bat]
@echo duck&%random% 2>nul

• Change the 1 to a 0 in case %random% returns 0? – Neil Mar 12 '18 at 17:31
• @Neil Done　　　　　　　　　　　　　　 – l4m2 Mar 12 '18 at 17:36

# Dirty, 20 bytes

Warning: May produce up to 2^63 "duck"s

#.⇖('duck'‼)'goose'‼


Try it online!

Explained:

#.⇖       generate a range up to a random number, put into left stack
('duck'‼) while left stack isn't empty, print "duck" and a newline
'goose'‼  print "goose" and a newline


# SmileBASIC, 31 25 bytes

?"duck
EXEC!RND(9)?"goose


Has a 1/9 chance of ending after each iteration.

## Explanation:

PRINT "duck"
EXEC !RND(9)
PRINT "goose"


SmileBasic has 4 "slots" that you can run code in. Normally programs run in slot 0, but you can load things like libraries into the others. EXEC <slot> is used to run code in a different slot.

RND(9) generates a random number from 0 to 8. If this is 0, !RND(9) is 1, otherwise it's 0.

EXEC 0 will run the current slot from the beginning (creating a loop), but EXEC 1 runs slot 1 (which is empty)

After running slot 1, execution returns to slot 0, and the program prints "goose" and ends.

# Vim + date, 33 29 bytes

:r!date +1\%SO␊D@"duck␛ogoose


Prints 100 to 159 ducks

## Explanation

:r!date +1\%SO␊        Get the current date in seconds, prepend it with and postpend an O
D@"aduck␛             Take the current line, cut it and use as code to insert duck the
specified amount of times
ogoose                 insert goose


# PHP, 35 bytes

duck
<?=time()%2?'duck
':''?>
goose


Or rand() if you prefer.

Try it online!

• I think this breaks the rule "There must be exactly one bird on each line. No empty lines." – Goose Mar 13 '18 at 18:50
• @Goose: There is a leading duck in the first line before the code. – Titus Mar 14 '18 at 15:54
• Save one byte with -n and date(s) instead of time()%2. – Titus Mar 14 '18 at 15:57
• @Tinus there's nothing in this code to makes new lines. Everything is on the same line. Every duck and goose must be on it's own line. – Goose Mar 14 '18 at 17:27
• @Goose works for me – Οurous Mar 14 '18 at 21:42

# Julia 0.6, 33 bytes

print("duck\n"^rand(1:9),"goose")


This will print duck up to 9 times before goose!

Try it online!

• Welcome to PPCG! – Martin Ender Mar 13 '18 at 10:08
• Welcome to PPCG! You can include a link to Try it online! (aka TIO) for your answer; TIO has a lot of interpreters and can automatically format your answer for you :) – Giuseppe Mar 13 '18 at 13:45

# F#, 74 bytes

let c=System.Random().Next()
for i=0 to c do
printfn"duck"
printfn"goose"


Try it online! - but note that the sample is limited to at most 20 ducks.

Setting i=0 means that if c is 0, then at least one "duck" will still be printed.

• Why using variable c and not just for i=0 to System.Random().Next()do printfn"duck" directly? – manatwork Mar 13 '18 at 15:15
• It would interfere with the randomness of it. Instead of i terminating at a random value, the larger i gets the more chance it has of being greater than a new random value. – Ciaran_McCarthy Mar 13 '18 at 15:35

# T-SQL, 44 bytes

a:PRINT'duck'IF RAND()<.5GOTO a PRINT'goose'


Different method than Alex' excellent answer.

# Pure Bash, 41 37 bytes

printf %s\\n ${RANDOM//[0-9]/duck } goose  This uses the same trick as in Sophia Lechner's answer to obtain a random number in the range 0-32767 with $RANDOM but does not require sed or any other external utilities to be installed to transform it into the desired result.

The Bash parameter expansion ${variable//pattern/replacement} obtains the value of variable with any match on pattern replaced by replacement. The shell then expands and tokenizes the arguments to printf, which applies the format string to each resulting argument. I'm not a good golfer. Digital Trauma's answer has a much more lenient pattern which gets me down to 37 bytes. printf %s\\n${RANDOM//?/duck } goose


Try it online!

# PHP, 36 bytes:

<?do{?>duck
<?}while(date(s))?>goose


Run with -n (no config file)

# Japt, 1817 16 bytes

Prints 1 or 2 ducks

goo iÝõ
pM¬Ä


Try it online!

Saved 1 byte thanks to Shaggy & 1 byte thanks to Oliver

### Explanation

goo      // compressed string "goose"
i    // prepend string
Ýõ // compressed string "duck\n"
// ...
p         // repeat n times
M¬       // random int 0 or 1

• 17 bytes – Shaggy Mar 13 '18 at 7:53
• 16 bytes – Oliver Mar 14 '18 at 18:01
• You can use p2â ö to randomly return 1 or 2. Unfortunately the shortcut for p2 wouldn't be useful here so it would still be 17 bytes. – Oliver Mar 14 '18 at 18:09
• Thanks @Oliver! I didn't catch that in the shortcuts. – powelles Mar 14 '18 at 20:44

# Forth, 91 BYTES

: DUCKDUCKGOOSE BEGIN 0 100 RANDOM 0 = IF ." GOOSE " LEAVE ELSE ." DUCK " THEN CR UNTIL ;


**OUTPUT: **

DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
DUCK
GOOSE  ok....

• Technically you need to add include random.fs also you can make it 67 bytes by using a do loop – reffu Aug 3 '18 at 15:42

# Befunge-93, 23 bytes

25*"kcud"#@,,,,,"goose?


Try it online!

Starts by printing "duck\n". The only way to generate randomness is with the ? instruction, which sets the PC's direction to one of the 4 possible directions, chosen at random. Up/down will wrap around vertically right back to the ? instruction, right will wrap around and print "duck\n" again, and left will print "goose" and terminate the program. The worst-case runtime is infinite, but the probability of the program exiting is 1.

With this code layout, we can reuse ,,,,, to print either string - luckily they are both of length 5. We can also omit the " after goose, which makes the stack leak with stringified code, but otherwise leaves it in a workable state, and all of the letters from the strings are practically no-ops when executed as code.

# Python 2, 54 bytes

Turns out thecoder16 ninja'd this Python 2 version...

from random import*
print"duck\n"*randint(1,9)+"goose"


Prints 1-9 ducks then a goose.

Try it online!

Originally...

Python 3,  61  56 bytes

from random import*
print("duck\n"*randint(1,9)+"goose")


Try it online!

• You can reach 54 bytes if you switch to py2 – Rod Mar 12 '18 at 16:45
• I think I may have just posted that, sorry – Quintec Mar 12 '18 at 16:48
• @thecoder16 Oh you ninja'd me? :D – Jonathan Allan Mar 12 '18 at 16:49
• It’s down there somewhere, about 3 minutes ago, though it’s not properly formatted because I don’t know how :( Should I delete or what? – Quintec Mar 12 '18 at 16:51
• @thecoder16 No, that's fine, I've hit the same solution as others within short time frames before - I only delete if I am much later and it's just a result of not reading other answers! (I've also linked to your post too) – Jonathan Allan Mar 12 '18 at 16:52

# FORTRAN 77, 121 120 bytes

      PROGRAM D
CALL RANDOM_NUMBER(A)
DO1WHILE I=1,A*9+1
1     PRINT*,'DUCK'
PRINT*,'GOOSE'
END


Prints up to ten nine ducks (thanks AdmBorkBork!).

Unfortunately, there is no syntax highlighting for Fortran. I can't understand why, since it is so popular and useful!

(Saved 11 bytes changing the Do-While syntax. Oh, such versatility!)

This program could have 92 bytes changing all the spaces in the indentation for tabs; it works using compiler gfortran, but I'm not sure it would work in older compilers. Since original FORTRAN 77 only accept spaces (because the punched card thing), I decided to not use tabs. Anyway, thanks Pavel for the suggestion!

# FORTRAN 77, 132 bytes

      PROGRAM D
CALL RANDOM_NUMBER(A)
DO 1,I=1,A*10+1
PRINT*,'DUCK'
1     CONTINUE
PRINT*,'GOOSE'
END

• Can you use tabs for indentation to save bytes? – Pavel Mar 12 '18 at 17:15
• I'm afraid Fortran 77 do not accept tabs, since the first columns in each line are reserved for labels, e.g., as you can see in the fifth line. Tabs are accepted (with warnings) since Fortran 90. – rafa11111 Mar 12 '18 at 17:17
• Can you save a byte using A*9 instead of A*10? – AdmBorkBork Mar 12 '18 at 18:52
• Wow, I'm ashamed! Pretty clever! Thank you! – rafa11111 Mar 12 '18 at 18:59

# Python 2, 65 bytes

from random import*
while random():print"duck"
print"duck\ngoose"


Try it online!

-6 bytes thanks to Rod

theoretically finishes. practically will probably never finish but eventually random.random() will spit out a 0

• I've not seen it finishing after a few tries... Theoretically it will is a fun one, tho ;) – AJFaraday Mar 12 '18 at 15:55
• 65 bytes – Rod Mar 12 '18 at 15:58
• I'm not totally certain this is valid, isn't it possible that for some seed the PRNG will actually never generate a zero? It's not a particularly big deal, since you can correct it easily, but I think this would technically need a proof. – FryAmTheEggman Mar 12 '18 at 17:26
• @FryAmTheEggman I think in this case "Never say never" is mathematically accurate. – Berry M. Mar 12 '18 at 18:41

I don't have the rep to comment on the other PowerShell answer, so I have to post it separately:

# PowerShell, 36 bytes

,'duck'*(random -mi 1 -ma 9);'goose'


Try it online!

My answer is longer, because the maximum flag is required. Omitting it often leads to an OutOfMemoryException: Array dimensions exceeded supported range. In this case the program does not output anything besides the error.

# Java 8, 8382726851 47 bytes

v->(Math.random()<.5?"duck\n":"")+"duck\ngoose"


Returns either one or two "duck" before "goose".

-1 byte thanks to @l4m2.
-4 bytes thanks to @someone.

Explanation:

Try it online.

v->                   // Method with empty unused parameter and String return-type
(Math.random()<.5?  //  If the random boolean is true:
"duck\n"          //   Return a "duck" + new-line
:                  //  Else:
"")               //   Return nothing
+"duck\ngoose";    //  Appended with a "duck", new-line and "goose"

• d+=.1 => d*=2 – l4m2 Mar 13 '18 at 10:00
• I made several searches before posting my answer, but I couldn't find this one, for some reason. I deleted it after seeing this one. Anyways, I'm not sure this fulfills the challenge as the challenge specifies "print", not "output". Edit... 2 pages, that's why I didn't see it... – Olivier Grégoire Mar 13 '18 at 11:00
• "Anyways, I'm not sure this fulfills the challenge as the challenge specifies "print", not "output"." Well, it also specifies program instead of function. I personally interpret 'print' as 'print or return' in almost all cases, except for explicitly STDOUT is mentioned. But maybe that's just me.. – Kevin Cruijssen Mar 13 '18 at 11:55
• I'm not familiar with Java, but this seems to work for 47 bytes. According to the docs, Math.random() returns values such that 0 <= x < 1, so Math.random()>0 should work too, but that seems hard to test and very unlikely to actually happen. – someone Mar 13 '18 at 13:20
• @someone The changes of an actual 0.000... are indeed astronomical small. But your 47-byte version is indeed a nice golf, so thanks! :) – Kevin Cruijssen Mar 13 '18 at 13:44

My name has never been more relevant.

PHP, 40 bytes

<?=str_repeat("duck\n",date("h"))?>goose


Only trick used is date("h") to generate a random number that will never be 0.

• Presumably you could just as easily use millisecond? – AJFaraday Mar 13 '18 at 19:26
• Probably a lot of options depending on how wide a range of random numbers you want, but I think they'd all be the same byte count. – Goose Mar 13 '18 at 20:19
• @Goose using mktime() saves one byte – Ben Mar 14 '18 at 1:58
• @Ben true, but it tries to allocate 7605159771 bytes and I can't run that on my machine so I'm not sure if it's appropriate to include. – Goose Mar 14 '18 at 12:55
• default values (no config; flag -n) disable notices and allow <?=str_repeat("duck\n",date(h))?>goose or <?=str_repeat("duck\n",date(h)),goose;, saving two bytes; a literal newline saves another byte. – Titus Mar 15 '18 at 21:43

# PowerShell, 57 bytes

$r=Random @(1..9) for($x=0;$x -lt$r;$x++){"Duck"}"Goose"  While$x is less than the value of \$r, write "duck", else write "goose".

First code golf I've done. Had fun with the challenge, and am looking forward to getting better!

• Welcome to PPCG! – Shaggy Mar 15 '18 at 0:50

# CJam, 17 bytes

es"duck
"*"goose"


Prints around 10^12 ducks. es is the builtin for Unix timestamp.

For two more bytes, a version with more reasonable output:

9mr)"duck
"*"goose"


## Groovy, 42 bytes

print'duck\n'*(1+Math.random()*10)+'goose'


Try it online!

• :) Yea kinda fun for golfing. – Gurupad Mamadapur Mar 16 '18 at 12:09

# Excel, 33 bytes

=REPT("duck
",9*RAND()+1)&"goose"


Prints duck between 1 and 10 times, followed by goose.

# T-SQL, 43 bytes

PRINT REPLICATE('duck
',RAND()*5+1)+'goose'


# Assembly (nasm, x64, Linux), 79 55 bytes

global main

main:
rdrand ecx
jnc main
.ducks:
mov al, 1      ; Move the syscall id into ral. Doing it using a single byte is shorter
mov edi, eax   ; Copy the value over, which takes exactly 2 bytes.
xor esi, esi   ; \
or rsi, duckD  ; Move a pointer to duckD into rsi. This is shorter than the equivalent move.
mov dl, 5      ; Move the size of the string into rdl.
push rcx       ; Preserve rcx for use by the loop instruction
syscall        ; Call the syscall. in this case, syscall write to file descriptor 1, aka stdout.
pop rcx        ; Restore rcx
loop .ducks    ; Decrement rcx and jmp back to .ducks if rcx isn't 0
mov al, 1
mov edi, eax
xor esi, esi
or rsi, gooseD
mov dl, 6
syscall
ret
duckD: db 'duck', 10, 0
gooseD: db 'goose', 10, 0



Try it online!

A simple explanation of syscall write is sitting here. Mostly for me, because I forget it exists sometimes.

WARNING: May print up to 2^32 ducks

There is a 25% chance after each duck that the loop will be broken and goose will be printed. This is because the X cell, which chooses a random cardinal direction. Every direction except right will skip over the lower segment due to how the ~ are placed.
>"kcud"WNojrXv