# 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

# 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". – user14823 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''  – user14823 Mar 12 '18 at 21:49 # Red, 38 bytes loop random 9[print"duck"]print"goose"  Try it online! # 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 # 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.

# Lua, 50 46 bytes

Giving a new language a try.

d="duck\n"print(d:rep(os.time()%9|1).."goose")


Try it online

# WinDBG, 39 bytes

.foreach(_ {#}){.echo duck};.echo goose


How it works:

.foreach(_                              $$Loop..., using an unused variable _ {#})$$ ...through each space separated "word" of the next disassembly text
{.echo duck};            $$Print duck on each iteration .echo goose$$ Print goose at the end


The dissambly text will be formatted something like this. The examples on MSDN don't have the first line, so maybe that only shows if you have symbols for the code that you're currently debugging. But definitely the assembly_details part can contain spaces (or be empty) which will cause a different number of ducks to print.

dll!Function [source @ line]:


With the above example, it would print (in parenthesis is the value of _, not actually printed)

duck (dll!Function)
duck ([source)
duck (@)
duck (line]:)
duck (binary_code)
duck (assembly_mnemonic)
duck (assembly_details)
goose


# Twig, 45 bytes

Twig is a templating language written in PHP.

This uses the random() function up to 9.
Then, it will loop all elements in the range of 1 - random(9).

This causes a bug: random(<n>) returns values up to the <n> value (including 0).
However, twig allows to make a range of 1 - 0, creating an array with 2 elements.

This ensures that there is always, at least, 1 duck.

{%for _ in 1..random(9)%}duck
{%endfor%}goose


You can try it on https://twigfiddle.com/w0m6p6

• I guess it´s a feature, not a bug. 0..random(9) and random(9)..0 should also work. – Titus Mar 15 '18 at 13:04
• Well, it could cause a bug. And yes, those work as well. – Ismael Miguel Mar 15 '18 at 13:30