# 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'.
• Your program ends.

### 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 finish.
• 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

# Python 2, 4843 33 bytes

print"duck\n"*(id(0)%9+1)+"goose"


Try it online!

Takes no input. Prints a max of 9 'duck's.

Explanation:

               # Call the id() function on the integer 0.
# This is a different, usually very large integer every time the program is executed.
# % 9 will return 0-8, so this will return 1-9.
id(0)%9+1
# print 1-9 'duck's, each ending in a new line, then finally 'goose'
print"duck\n"*(.........)+"goose"


# Ink, 24 22 bytes

-(h)
duck
{~goose|->h}


Try it online!

# MathGolf, 1716 15 bytes

9╒wôÿduckn'g╕3


-1 byte thanks to @maxb.

Try it online.

Explanation:

Prints [1, 9] amount of duck.

9╒          # Push a list [1,2,3,4,5,6,7,8,9]
w         # Pop and push a random integer from this list
ô        # Loop that many times and execute the following six characters:
ÿduck   #  Push the 4-character string "duck"
n  #  Push a newline
'g╕3   '# After the loop: push 'g' and compressed "oose"
# (join everything on the stack together and output it implicitly)

• To get a random number in range [1, 9] inclusive: 9╒w. This way, you don't have to discard the stack after the loop. 15-byter: 9╒wôÿduckp'g╕3 – maxb Jun 3 at 13:28
• @maxb Ah, of course! I initially had 9w, but of course it could also result in 0, so I changed it to fú and combined it with ╒, but was kinda annoyed by the integers still on the stack. But of course 9╒w is better, fixing that problem. :) – Kevin Cruijssen Jun 3 at 13:47

# C (gcc), 666553 50 bytes

main(){for(;puts("duck")&time(0););puts("goose");}


Try it online!

• I can print one or two ducks in 46 bytes if you're interested... – Neil Mar 12 '18 at 16:15
• @Neil Post your answer – cleblanc Mar 12 '18 at 16:21
• main(){puts("duck\nduck\ngoose"+time(0)%2*5);} – Neil Mar 12 '18 at 16:33
• I think as puts returns 5 all the time (for this case), so for(;time(0)&puts("duck");) may work (although in a different way) – user202729 Mar 12 '18 at 16:37
• If you use puts("duck")&time(0) u should say the environment. Some puts() return 0 – l4m2 Mar 12 '18 at 17:19

# J: 24 bytes

Arbitrarily limited to no more than 9 ducks:

'Goose',~>(>:?9)$<'Duck'  • I've just run this through Try It Online. It doesn't seem to output anything. tio.run/##y/r/X909P784VV2nzlrDzsreUlPFRt2lNDlbR0H9/38A – AJFaraday Mar 12 '18 at 16:58 • You need to use echo to get the output (presumably this is assumed to be run on the REPL). Try this. I don't know why it seems like it only outputs one possible number of "Duck"s, though (probably has to do with the way J evaluates ? in nouns). – cole Mar 12 '18 at 17:08 • TIO seems to prescribe a particular format not suited to an immediate execution language like J (or their J implementation is broken). Try putting the code both in "Code" and "Input". – DevonMcC Mar 12 '18 at 17:14 • J seeds the RNG with 16807 on startup, so the output is always deterministic, noun or not. Try it with seeding – FrownyFrog Mar 12 '18 at 20:41 • According to Dennis at the tio site, you need to prefix the J code with "echo" to see the output. To re-seed the RNG, you could add this phrase to the right of a J phrase using random numbers: [ 9!:1]<.13#.|.}.6!:0''  – DevonMcC Mar 12 '18 at 21:49 # Red, 38 bytes loop random 9[print"duck"]print"goose"  Try it online! # Aceto with -l flag, 25 bytes R99**i"duck\n"*£p"goose"p  Try it online! • Command-line flags are counted as separate languages, now, so no need to add +1 to your byte count. – AdmBorkBork Mar 12 '18 at 19:09 • @AdmBorkBork Wasn't aware. Thanks! – drham Mar 12 '18 at 19:24 # Chip, 68 bytes Uses flag -w. (Turns out we've decided to simplify how we count flags.) ! >----z/v\ZZZZZt >ZZZZ-xL')))xc xx)))b? b((( )x-x)~a dde c^ed^~g  Try it online! After printing each "DUCK\n", makes a binary choice for whether to go back to "DUCK\n", or print "GOOSE" and terminate. So, 1/2 of all runs will see one DUCK, 1/4 will see two, 1/8 will see three, and so on. The left blob under the Z's contains the encoding for "DUCK\n", and the right blob under the Z's contains "GOOSE". The connecting bit between the two makes the random decision, and the rest is just infrastructure. # Add++, 42 bytes D,f,?:,@n R9 y:duck W,x:y,O,x:0,R9 x:goose  Try it online! Ruby, 49 Bytes (rand(9)+1).times do puts "duck" end puts "goose"  Explanation 1. Generate an integer between 0 and 9 and add 1 to it 2. Each of those times print a duck 3. End the loop and print goose • With 0.upto(rand 9){puts:duck};puts:goose you can get rid of the extra parenthesis needed due to the +1. – manatwork Mar 13 '18 at 13:41 # RUST, 240 bytes extern crate rand; use rand::distributions::{IndependentSample, Range}; fn main() { let mut rng = rand::thread_rng(); println!("{}", std::iter::repeat("Duck\n").take(Range::new(1, 9).ind_sample(&mut rng)).collect::<String>()+"Goose"); }  You can try it here: https://play.rust-lang.org/?version=stable&mode=release Copy and Paste Sorry I dont have shorten link due their server error! • Hi there -- I don't know RUST at all but there's definitely some unnecessary whitespace you can remove. If there's any way to make shorter variable names, that will also help. – Giuseppe Mar 13 '18 at 17:42 • play.rust-lang.org/… 114 bytes – Noskcaj Mar 15 '18 at 1:48 • play.rust-lang.org/… 101 bytes – Noskcaj Mar 15 '18 at 22:07 # Ruby: 33 bytes puts 'duck\n'*Time.new.sec+'goose'  Try it online! (Note that TIO doesn't respect newline characters like a normal console) It might take a second, but sooner or later it'll print another number of ducks... Okay, I just like the idea, it'd be a little shorter as: # Ruby, 30 bytes Shortened thanks to @ovs puts"duck\n"*-~rand(9)+'goose'  Try it online! • Ah, you've bested my Ruby version by a byte! I have had to resort to a cheat to get down to 30 (which if you apply would get you to 29). – Turophile Mar 14 '18 at 4:42 • That's 34 bytes, not 33. – Shaggy Mar 15 '18 at 20:01 • @Shaggy I got there with Ruby’s ‘length’ function. Perhaps it’s reading the newline character as one? – AJFaraday Mar 15 '18 at 22:31 • Note that TIO doesn't respect newline characters like a normal console This is not a TIO issue. \n works inside double quotes, but not inside single quotes. In any case, \n can be replaced with a literal newline. Try it online! – Dennis May 16 '18 at 15:20 # mIRC v.7.51, 40 37 bytes Also works in most older versions, and definitely in newer versions. //echo$str(Duck $crlf,$r(1,9)) Goose


How It Works?

$r(1,9) -> Generates a random number between 1 and 9$crlf -> Prints the carriage return and linefeed characters (ASCII 13 and 10 respectively)
$str -> Reoeats the first argument required number of times echo -> Prints the succeeding text in the window  -3 bytes -> Changed $rand to $r Please note that mIRC echo does not recognize the newline character, so the text is printed in the same line in the window. However, the text contains the newline character which means that if you copy paste it anywhere, then you will see the text in multiple lines. I believe this is allowed. # AWK, 59 55 bytes END{for(srand();i++<9*rand();)print"duck";print"goose"}  Try it online! -4 bytes thanks to manatwork • No need for parenthesis around print's argument. – manatwork Mar 13 '18 at 12:37 ### Javascript - 61 bytes document.write('duck<br>'.repeat(Math.random()*10+1)+'goose')  Live here ### Second change - 57 bytes document.write('duck<br>'.repeat(new Date()%9+1)+'goose')  Thanks @Martin Another way is using console.log, but it is not visual for people to see the answer ### 52 bytes console.log('duck\n'.repeat(new Date()%9+1)+'goose')  Try it online ### 46 bytes with alert alert('duck\n'.repeat(new Date()%9+1)+'goose')  Try it online • Welcome to PPCG! You don't need to print duck so many times, so you can save a byte by reducing *10 to *9 or something like that. And I think a common way to get a shorter indeterminate number would be new Date()%9+1. – Martin Ender Mar 13 '18 at 12:27 • @MartinEnder Thanks, I think about number 10 and 9 before but I forgot to try that number, and thanks for your suggestion about new Date()%9+1 that helped me saved some bytes, I will update the code – Hyperfork Mar 13 '18 at 12:32 • The 52-byte version is completely fine. That's the usual output you'd use in a console (as opposed to a browser): tio.run/##y0osSyxOLsosKNHNy09J/f8/… – Martin Ender Mar 13 '18 at 13:00 • I have added a better solution with "alert", It only 46 bytes. – Hyperfork Mar 13 '18 at 13:10 • save another 4 bytes by using lambda: _=>*duck *.repeat(new Date()%9+1)+'goose' (literal newline after duck and  instead of *) – Brian H. Mar 14 '18 at 12:27 ## PHP 53 45 bytes echo str_repeat("duck\n",rand(1,99))."goose";  -8 bits thanks to @manatwork Try it online! • Wouldn't rand(1,99) do essentially the same as substr(rand(),-2)? – manatwork Mar 13 '18 at 12:20 • @manatwork yep, didn't know that function existed, thanks – Luis felipe De jesus Munoz Mar 13 '18 at 13:10 # Pyth, 17 bytes VhOT"duck";"goose  Try it online! Pretty simple. Loops between 1 and 11 times, printing duck, then prints goose. # Golfscript, 22 bytes 9rand)"duck\n"*"goose"  • (completely new to golfscript, this may not be optimal) – Quintec Mar 13 '18 at 14:17 # Elixir, 57 bytes fn->String.duplicate("duck ",:rand.uniform 2)<>"goose"end  Returns a string either one or two ducks. Try it online! # Tcl, 45 characters while {rand()<.9} puts\ duck puts duck\ngoose  Try it online! ### Tcl, 55 characters puts [string repeat duck\n [expr int(rand()*9+1)]]goose  Sample run: bash-4.4$ tclsh <<< 'puts [string repeat duck\n [expr int(rand()*9+1)]]goose'
duck
duck
goose


Try it online!

# Attache, 33 bytes

Print=>Random[1,9]&"duck"'"goose"


Try it online!

## Explanation

Print=> prints each element of:

Random[1,9]&"duck", which repeats "duck" a random number between 1 and 9 times, and

"goose", which is appended to the end with '.

# SNOBOL4 (CSNOBOL4), 76 bytes

	X =TIME() * 100
S	OUTPUT ='DUCK'
X =GT(X) X - 1	:S(S)
OUTPUT ='GOOSE'
END


Try it online!

The TIME function counts the amount of time (in tenths of a second) elapsed since the program began, which does have a bit of randomness. Since it's so short, however, we have to scale by 100 or else the time will be nearly always zero. Then it outputs DUCK until X<0, decrementing X each iteration, before printing GOOSE.

# Funky, 51 bytes

(p=print)"Duck"whilemath.random()>.3p"Duck"p"Goose"


Funky is surprisingly bad at Duck Duck Goose.

Try it online!

## shell, 65 bytes

f(){ echo duck&&expr date +%N % 2 >/dev/null&&f||echo goose;};f


ideone

• Too many 2 character list operators, when the single character ; would be enough. Also discarding expr's output would be shorter with an assignment than with redirection: f(){ echo duck;x=expr \date +%N\ % 2&&f;};f;echo goose – manatwork Mar 14 '18 at 9:52
• @manatwork Thank you for your advice. Just have fun. – Divlaker Mar 15 '18 at 8:53

# golflua, 38 characters

M.rs(O.t())!@w"duck"!$M.r()<.1w"goose"  Sample run: bash-4.4$ opt/eso/golflua-1.0/src/golflua -e 'M.rs(O.t())!@w"duck"!$M.r()<.1w"goose"' duck duck goose  ## Perl6/Rakudo, 27 26 chars say ｢Duck ｣x now%9~'Goose'  Change now%9 to 9.rand for sub-second nondeterminism at a cost of 1 char. # Labyrinth, 65 bytes " ^ 1 \.701 0 . 0.117.99 0 1 " """v " 103.11:1:..5.101.@  Try it online! # PHP 55 53 Bytes (Thanks manatwork) for($i=0;$i<time();$i++)echo 'duck<br>';echo 'goose';


Legibly

for($i=0;$i<time();\$i++){
echo 'duck<br>';
}
echo 'goose';


Now, I haven't tested this, because I don't want to melt my old computer, but it will output one duck for every second since January 1, 1970, 0000 hours. (Then a goose)

• Checked the documentation of mktime()? The Notes contains a good golfing hint: mktime() without parameter is equivalent with time(). – manatwork Mar 14 '18 at 8:40

# PHP, 40 bytes

<?=str_pad(goose,5+5*date(m),"duck
",0);


prints one duck for every month (3 in March, 4 in April etc.)

Run with -n (no config file) or try it online.

# APL+WIN, 24 bytes

⊃((?⎕),1)/'duck' 'goose'
`

Prompts for an integer which can be between 1 and system maximum and will then select a number from 1 to that integer at random for the number of ducks. If no input is desired then the max integer can be hard coded for a few more bytes.