59
\$\begingroup\$

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.
\$\endgroup\$
14
  • 11
    \$\begingroup\$ Could you define indeterminate? Could it mean either zero or one? \$\endgroup\$
    – recursive
    Mar 12 '18 at 15:51
  • 1
    \$\begingroup\$ Please define how randomly this should be generated. Uniform in range or with exponential decline? \$\endgroup\$
    – hyper-neutrino
    Mar 12 '18 at 15:52
  • \$\begingroup\$ @recursive Nope, but let's go with a working definition... The program does not consistently present the same number of ducks. \$\endgroup\$
    – AJFaraday
    Mar 12 '18 at 15:52
  • 2
    \$\begingroup\$ Speaking as a Minnesotan, what if mine prints "duck, duck, gray duck" instead? :) \$\endgroup\$
    – Mike Hill
    Mar 14 '18 at 19:13
  • 1
    \$\begingroup\$ @jpmc26 I’m sure there are others. You’d have had to play it with others, for a start. \$\endgroup\$
    – AJFaraday
    Mar 15 '18 at 23:13

119 Answers 119

44
\$\begingroup\$

Operation Flashpoint scripting language, 48 bytes

f={s="duck\n";s+([s,""]select random 1)+"goose"}

Always prints either one or two ducks.

random 1 returns a (floating point) number between 0 and 1. That number is passed as an argument to select along with the array [s,""]. The random number is then rounded to the nearest integer (either 0 or 1), and the array element at that index is selected from the array.

Call with:

hint call f

Output:

Alternative 56 bytes version:

f={s="duck\n";format[s+"%1goose",[s,""]select random 1]}
\$\endgroup\$
2
  • 3
    \$\begingroup\$ It always makes me glad when someone uses this on PCG. \$\endgroup\$
    – Etheryte
    Mar 13 '18 at 20:03
  • \$\begingroup\$ Well i forgot this game existed. \$\endgroup\$
    – Caimen
    Mar 16 '18 at 19:33
39
\$\begingroup\$

World of Warcraft 81 Bytes

Here's a macro that you can run in World of Warcraft.

/run for x=1,random(1,9) do SendChatMessage("Duck") end; SendChatMessage("Goose")
\$\endgroup\$
4
  • 11
    \$\begingroup\$ Oh my lord... Why have I never thought of this. \$\endgroup\$ Mar 13 '18 at 19:03
  • 4
    \$\begingroup\$ PVCG (Programming Videogames and Code Golf). Operation Flashpoint was a thing, but WoW? I am waiting the Minecraft and Factorio solutions of this. \$\endgroup\$ Mar 14 '18 at 10:59
  • 2
    \$\begingroup\$ @AnderBiguri I suspect the Minecraft one would do a little worse than 81 bytes (blocks?) ;) \$\endgroup\$
    – Chris
    Mar 14 '18 at 19:24
  • 5
    \$\begingroup\$ This should work and get you down to 58 bytes: /run for x=1,random(1,9)do print("Duck")end print("Goose") \$\endgroup\$
    – gastropner
    Mar 15 '18 at 10:33
38
\$\begingroup\$

Minecraft <1.13, 72 54 bytes

Sorry, I had to.

Instructions:

  • Create a new Minecraft world in Creative Mode
  • Find the save folder for that world, and place the following code in data/functions/minecraft/ddg.mcfunction
  • Run /function ddg in the game console

How it works:

Outputs the word "duck" for every entity in the world, then outputs the word "goose". Since entities are constantly spawning and despawning, the number of "duck"s will not be consistent. I used tellraw instead of the much shorter say because say outputs the name of the entity, while tellraw outputs exactly what it is told.

execute @e ~ ~ ~ tellraw @a "duck"
tellraw @a "goose"

Screenshot

enter image description here

Edit: Changed {"text":"duck"} to just "duck" (and the same with "goose")

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Beautiful. We need more Minecraft in PPCG. \$\endgroup\$
    – BradC
    Mar 15 '18 at 19:23
  • 9
    \$\begingroup\$ Worth mentioning that you (the player) are an entity in the world, so the number of ducks can never be 0 (even if there were no hostile or passive mobs). \$\endgroup\$
    – BradC
    Mar 15 '18 at 21:56
  • 1
    \$\begingroup\$ @BradC there should be entire challenges based around Minecraft. \$\endgroup\$
    – tox123
    Mar 18 '18 at 17:47
17
\$\begingroup\$

JavaScript, 45 44 42 39 37 bytes

Has the potential to produce an overflow error.

f=_=>`duck
${new Date%2?f():`goose`}`

Test it

o.innerText=(
f=_=>`duck
${new Date%2?f():`goose`}`
)()
<pre id=o></pre>

\$\endgroup\$
0
14
\$\begingroup\$

Octave, 38 33 bytes

This is not the shortest (it's 36 bytes), but it's my favorite. The explanation is in the bottom.

disp(['duck '+~(1:1/rand)';'goose'])

Try it online!


Some shorter variations:

This works in principle (33 bytes), but the online interpreters times out:

disp(['duck '+~(1:now)';'goose'])

Adding some bytes to make the output shorter makes it 35 or 36 bytes:

disp(['duck '+~(7e5:now)';'goose'])   % Works on octave-online.net
disp(['duck '+~(7.3e5:now)';'goose']) % Works on tio.run

Explanation:

I'll just explain the last random one. The others are similar, but uses the number of days since January 1st, 0000 until today.

rand returns a random number on the interval (0, 1). Thus, 1/rand returns a number larger than 1. Since a range 1:f, where f is a random float larger than 1 is identical to 1:floor(f), 1:1/rand creates a range 1 .. x, where x >= 1.

I'll try to explain this as if Octave was a stack based language.

      'duck '                           % Push the string 'duck ' to the stack
               (1:1/rand)               % Push a vector 1... floor(1/rand)
              ~(1:1/rand)'              % Negate the vector (making it all zeros)
                                        % and transpose it.
             +                          % Add 'duck ' with the vertical vector of zeros
                                        % to implicitly duplicate the string r times
     [                    ;'goose']     % Push the string 'goose' and concatenate
                                        % vertically with the rest
disp(                              )    % Display it all.
\$\endgroup\$
1
  • \$\begingroup\$ disp(['duck '+~pwd';'goose'])? Not sure if this is "indeterminate" enough \$\endgroup\$
    – Luis Mendo
    Mar 12 '18 at 19:53
12
\$\begingroup\$

Perl 5, 20 bytes

First a practical 26 bytes:

Ducks 1 to 9 times before being goosed.

say"Duck
"x(1+$^T%9),Goose

Try it online!

But if you have lots of memory and time this 20 byte version (as suggested by Chris) works too:

say"Duck
"x$^T,Goose

This also assumes the Year 2038 Problem will be solved for Perl 5, otherwise it will be invalid for 1 second 20 years hence.

\$\endgroup\$
2
  • \$\begingroup\$ If you change 1+$^T%9 to just $^T, it still works, and you save 4 bytes. \$\endgroup\$
    – Chris
    Mar 13 '18 at 1:47
  • \$\begingroup\$ @chris You obviously have more memory than I do. Still, I'll take your suggestion since there are reasonable computers on which it will work. Thanks. \$\endgroup\$
    – Ton Hospel
    Mar 13 '18 at 6:03
12
\$\begingroup\$

Python 2, 36 34 bytes

print"duck\n"*((id(id)%5)+1),"goose"

Try It Online!

Suggestion by Kevin Cruijssen gets us to 34 bytes:

print"duck\n"*-~(id(id)%5),"goose"
\$\endgroup\$
7
  • 3
    \$\begingroup\$ Welcome to the site, and nice first answer! \$\endgroup\$ Mar 12 '18 at 20:30
  • \$\begingroup\$ Welcome to PPCG! You can golf two bytes by changing ((id(id)%5)+1) to -~(id(id)%5) to get rid of the parenthesis. Tips for golfing in <all languages> and Tips for golfing in Python might be interesting to read through. Enjoy your stay! \$\endgroup\$ Mar 13 '18 at 11:50
  • 1
    \$\begingroup\$ Can id(id) not be id(0) or am I missing something? \$\endgroup\$ Mar 14 '18 at 11:18
  • \$\begingroup\$ @Johnathan, looking at id(0) on a couple different computers suggests it's a constant. \$\endgroup\$
    – user2699
    Mar 14 '18 at 18:21
  • 1
    \$\begingroup\$ Slightly more intuitive and no more bytes would be to call id([]). This should initialize a distinct list each time the program runs, which in theory should consistently result in different memory addresses. \$\endgroup\$
    – jpmc26
    Mar 15 '18 at 23:17
12
\$\begingroup\$

Z Shell (+ wget & Netpbm), 168 160 150 148 145 135 120 bytes

d(){wget -O- bit.ly/$1|jpegtopnm|pamscale -w 64 -h 64};d DckDkGo|pnmtile 64 $[(RANDOM&7+1)*64]|pnmcat -tb - <(d g005eGG)

Not the shortest solution, but I felt like giving a twist of sorts to this challenge (inspired by @AlexG's solution to this other problem).

This script generates a PPM image containing between 1-8 pictures of ducks and a picture of a goose at the bottom on standard output. It downloads the two source pictures from Wikipedia, so internet access is necessary for it to work.

Sample output converted to JPEG through pnmtojpeg:

enter image description here

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This has totally made my day! \$\endgroup\$
    – AJFaraday
    Mar 23 '18 at 8:46
  • \$\begingroup\$ I've just realized that you're the same person who proposed that other challenge I linked to. xD Well, I'm glad you liked this! =D \$\endgroup\$
    – lucasb
    Mar 23 '18 at 16:47
  • \$\begingroup\$ Yep, there's some themes in my code golf challenges :) I should definitely give bonus points for pictoral answers. \$\endgroup\$
    – AJFaraday
    Mar 23 '18 at 16:48
  • \$\begingroup\$ Isn't this a standard loophole, to get external resources? \$\endgroup\$
    – MilkyWay90
    Jan 12 '19 at 16:56
11
\$\begingroup\$

Bash, 39 38 37 bytes

sed s/[0-9]/duck\\n/g<<<$RANDOM\goose

Try it online!

Prints a number of ducks equal to the number of digits in an integer uniformly distributed on [0,32767] (so, more often than not, five ducks (a good number of ducks)).

-1 byte each thanks to @Chris and @sch pointing out pairs of quotes that weren't pulling their weight.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Steadybox
    Mar 12 '18 at 19:31
  • 1
    \$\begingroup\$ You can save one byte by getting rid of the single quotes as long as you replace \n with \\n. \$\endgroup\$
    – Chris
    Mar 13 '18 at 1:55
  • 1
    \$\begingroup\$ Nice idea. You can do something similar with just bash constructs and no sed for 30 bytes. \$\endgroup\$ Mar 13 '18 at 6:27
  • 1
    \$\begingroup\$ sed s/[0-9]/duck\\n/g<<<$RANDOM\goose can shave off one byte \$\endgroup\$
    – sch
    Mar 13 '18 at 16:49
  • 1
    \$\begingroup\$ The sed isn't entirely portable. On some platforms you might get away with a single backslash in \n. On others you won't get a newline no matter what you do. \$\endgroup\$
    – tripleee
    Mar 13 '18 at 19:33
10
\$\begingroup\$

R, 35 bytes

cat(rep("duck
",rexp(1)+1),"goose")

Try it online!

rexp() produces a random number from an exponential decay function. +1 to ensure at least one duck. All lines after the first include a leading space (which is the default separator for cat) but this is allowed.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ The probability of outputting more than one duck is exp(-1) or around 36.8%. \$\endgroup\$
    – Giuseppe
    Mar 12 '18 at 16:50
  • \$\begingroup\$ It asks to print it on each line. I think you should add " \n" to your "duck" \$\endgroup\$ Mar 13 '18 at 10:54
  • 3
    \$\begingroup\$ @stuartstevenson Here I have used a literal newline rather than \n as it is one byte shorter. If you visit the "Try it online!" link you can see that the effect is the same. \$\endgroup\$ Mar 13 '18 at 11:02
  • \$\begingroup\$ On a DOS/Windows box, that newline would still be two (2) bytes. 0x0d0a \$\endgroup\$
    – lit
    Mar 17 '18 at 19:16
10
\$\begingroup\$

Pure Bash (no external utilities), 25

Based on @SophiaLechner's answer, this also prints a good number of ducks.

@OlivierDulac's idea to use the script shell PID as stored in the $ parameter saves 5 bytes.

echo "${$//?/duck
}"goose

Try it online.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ if we assume you launch a program, it will get a different pid every time. a lot of the time it will be 5 digits long, but not always. hence: change RANDOM to $ to shave 5 bytes?And one could also do a very long string of ducks: yes duck|head -n $$;echo goose \$\endgroup\$ Mar 14 '18 at 16:28
  • \$\begingroup\$ @OlivierDulac yes, I think using $$ is acceptable - thanks! \$\endgroup\$ Mar 14 '18 at 17:03
  • \$\begingroup\$ you are welcome! I posted my other version (yes duck | head -n $$;echo goose) as an alternative (longer than yours, but one gets a LOT of ducks for 5 extra chars ^^) \$\endgroup\$ Mar 14 '18 at 17:29
9
\$\begingroup\$

Jelly, 13 bytes

2X“¢;ÆS»ẋ“ʋ¢»

Try it online!

Explanation:

2X“¢;ÆS»ẋ“ʋ¢»

2X             Random number (1 or 2)
  “¢;ÆS»       Compressed string equivalent to "duck\n"
        ẋ      Repeat the "duck\n" that random number of times
         “ʋ¢»  Compresses string equivalent to "goose"
               Implicitly concatenated and returned

More readable version: Try it online!

Will always return 1 or 2 ducks.

\$\endgroup\$
7
  • \$\begingroup\$ Congrats! I have literally no idea how this works! \$\endgroup\$
    – AJFaraday
    Mar 16 '18 at 10:19
  • \$\begingroup\$ @AJFaraday I went ahead and added an explanation for you \$\endgroup\$
    – PunPun1000
    Mar 16 '18 at 13:07
  • \$\begingroup\$ @PunPun1000 Sorry, but what's the stack? Jelly is a tacit programming language. \$\endgroup\$
    – DELETE_ME
    Mar 16 '18 at 13:51
  • \$\begingroup\$ @user202729 Oops, I got Jelly and 05AB1E messed up in my head, could you please edit it to fix that error? Sorry about that \$\endgroup\$
    – PunPun1000
    Mar 16 '18 at 14:02
  • \$\begingroup\$ There's an implicit concatenation in Jelly? Yay! \$\endgroup\$
    – MilkyWay90
    Mar 21 '19 at 23:38
9
\$\begingroup\$

Ruby, 30 bytes

puts"duck
"*rand(1..9)+"goose"

Try it online!

Note: really 31 bytes without the \n cheat.

\$\endgroup\$
3
  • \$\begingroup\$ I’m not sure it’s a cheat, strictly speaking. \$\endgroup\$
    – AJFaraday
    Mar 14 '18 at 6:44
  • \$\begingroup\$ maybe you could do *(1/rand) to shed 2 bytes. this could fail in the case rand returns 0.0, but that seems astronomically unlikely. or well, i tried 100_000_000.times { puts 'zero!' if rand.zero? } and didn't get a zero, so i'd be OK to deploy this to prod :) (sorry, i deleted a previous comment unintentionally while trying to edit it) \$\endgroup\$
    – epidemian
    May 28 at 15:54
  • \$\begingroup\$ @epidemian Answers with extremely small chances of malfunctioning are not allowed \$\endgroup\$ Jun 7 at 23:02
9
\$\begingroup\$

Bash + Coreutils, 36 27 bytes

yes duck|sed $$q;echo goose

Prints too many ducks (between 2 and cat /proc/sys/kernel/pid_max), then one goose.

Saved nine bytes thanks to Digital Trauma and Olivier Dulac.

Try it online! (but keep in mind that it may get truncated occasionally)

Same length, but with no echo:

yes duck|sed $${agoose'
q}'

Try it online!

a is the append command in sed, and q quits. Both only run on the line $$, which corresponds to the PID.

\$\endgroup\$
9
  • \$\begingroup\$ Bash has modulo, though that will increase the size. Try head -n $((1+(RANDOM % 5))) \$\endgroup\$
    – tripleee
    Mar 13 '18 at 20:12
  • \$\begingroup\$ @tripleee Since this is code golf, increasing the size of the output is preferable to increasing that size of the code. And 132767 ducks is downright reasonable compared to the billion or more you see elsewhere ;) \$\endgroup\$
    – Chris
    Mar 13 '18 at 20:16
  • 1
    \$\begingroup\$ Sure, just hoping to be helpful in case somebody actually wants to apply this groundbreaking research in practice one day. Like, TIO. 44 bytes with some spaces taken out. \$\endgroup\$
    – tripleee
    Mar 14 '18 at 5:18
  • \$\begingroup\$ @OlivierDulac suggested using $$ instead of $RANDOM for my answer. I think you could use the same to save you 5 bytes. Oh and try sed too: yes duck|sed 1$$q;echo goose \$\endgroup\$ Mar 14 '18 at 17:10
  • \$\begingroup\$ In fact yes duck|sed $$q;echo goose is fine - I don't think your script would ever get a PID < 1. \$\endgroup\$ Mar 14 '18 at 17:17
8
\$\begingroup\$

PowerShell, 35 30 28 bytes

,"duck"*((Random)+1)
"goose"

Try it online! (modified version)

Generates an array of Get-Random number of items. It might take a while. This adds a +1 to ensure we get at least one duck. The modified version also includes a -maximum flag of 5 so you can see the program works as expected (the modified version will print 1, 2, 3, or 4 ducks before the goose).

The array and the solitary goose string is left on the pipeline, and the implicit Write-Output gives us newline-separated values for free.

You don't know how difficult it was for me to not change the last line to "gray duck" ...

\$\endgroup\$
4
  • \$\begingroup\$ That variant sounds pretty fun, and definitely worth a code challenge... I recommend you wait a couple of days and post it as a challenge of your own :) \$\endgroup\$
    – AJFaraday
    Mar 12 '18 at 16:12
  • 1
    \$\begingroup\$ @AdmBorkBork, this Minnesotan joined this group to just +1 your gray duck. \$\endgroup\$
    – Milwrdfan
    Mar 12 '18 at 17:06
  • \$\begingroup\$ 0..(random)|%{'duck'};'goose' looks like a 29, and will also generate somewhere up to [int]::MaxValue (2.1 billion) ducks before a goose. (And 0..0 does print a duck) \$\endgroup\$ Mar 16 '18 at 8:17
  • \$\begingroup\$ @TessellatingHeckler Thanks, but I just-now came up with a 28-byte version. :) \$\endgroup\$ Mar 16 '18 at 12:32
6
\$\begingroup\$

Python 2, 54 bytes

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

Try It Online!

\$\endgroup\$
1
  • \$\begingroup\$ @JonathanAllan thanks :) \$\endgroup\$
    – Quintec
    Mar 12 '18 at 19:09
6
\$\begingroup\$

C# (Visual C# Interactive Compiler), 55 bytes

Write("{"+new Random().Next(2)+"}{0}goose","duck\n","")

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Really clever use of string formatting! \$\endgroup\$
    – Gymhgy
    Apr 30 '19 at 2:46
6
\$\begingroup\$

Brachylog, 24 21 bytes

-3 bytes thanks to Erik the Outgolfer

"duck"ẉ4ṙ0∧"goose"w∨↰

Try it online!

In celebration of the Language of the month, my first brachylog post. The control flow in this language is cool.

How it works:

"duck"ẉ4ṙ0∧"goose"w∨↰
"duck"ẉ                 print duck with a new line
       4ṙ               choose a random number in the range is [0, 4]
         0              verify it equals zero
          ∧             and (short circuits)
           "goose"w     print goose without a newline
                   ∨    or (if it did not equal zero)
                    ↰   repeat the procedure
\$\endgroup\$
4
  • \$\begingroup\$ So, if I’m reading this right, it reads from left-to-right, bottom-to-top? \$\endgroup\$
    – AJFaraday
    Mar 13 '18 at 21:33
  • 2
    \$\begingroup\$ The language attempts to find a logical truth using just the first line of the program. My program says "Validate the second line, then print goose". Then the second line says "print duck, then validate either a random number 0 to 4 is 0, or validate this line again", so technically left to right just the top line, then any predicates you call from there \$\endgroup\$
    – PunPun1000
    Mar 13 '18 at 21:36
  • \$\begingroup\$ Cool, it’s so interesting finding out how new languages work. It did reach my inbox, but I’m not sure if it’s because I posed the question or because I’d already commented here. \$\endgroup\$
    – AJFaraday
    Mar 13 '18 at 21:49
  • \$\begingroup\$ You can merge the two predicates like this and tie me. \$\endgroup\$ Mar 13 '18 at 23:10
6
\$\begingroup\$

Geometry Dash World 2.2 Editor - 4 objects

Picture of Geometry Dash Level

Explanation:

The BG trigger is the current 2.2's random trigger, so it either toggles the Group ID 1 or 2.

The first "DUCK" has a group id of 1, which makes it have a 50% chance of being removed or not (toggled).

There is no object with the Group ID 2 in this level, so there is a 50% chance of 2 "DUCK"s being displayed.

How to Reproduce this:

The first "DUCK" has a Group ID of 1.

enter image description here

Goose and 2nd duck don't have a Group ID enter image description here

Inside the random trigger. enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ I sense a theme for this challenge \$\endgroup\$ Mar 18 '19 at 23:09
  • \$\begingroup\$ @BenjaminUrquhart What? \$\endgroup\$
    – MilkyWay90
    Mar 18 '19 at 23:10
  • \$\begingroup\$ There are submissions for Word of Warcraft, Minecraft, and some other game as well. \$\endgroup\$ Mar 18 '19 at 23:11
  • 2
    \$\begingroup\$ @BenjaminUrquhart Better find more games to answer in \$\endgroup\$
    – MilkyWay90
    Mar 18 '19 at 23:12
5
\$\begingroup\$

05AB1E, 15 14 bytes

'Ðœ₁Ωи`.•zíΘ•»

Try it online!

Will print 2, 5 or 6 ducks and then goose.

-1 byte thanks to @Emigna using ' for a single compressed word (duck)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can use 'М since duck is a single word. \$\endgroup\$
    – Emigna
    Mar 12 '18 at 16:39
  • 1
    \$\begingroup\$ Beats mine: ['ÐœTΩ#].•zíΘ•» \$\endgroup\$ Mar 13 '18 at 13:20
  • \$\begingroup\$ @MagicOctopusUrn I really like your infinite loop approach. Your answer is definitely more in the spirit of the challenge (or what it should have been ?) since it can output an infinite amount of ducks. \$\endgroup\$
    – Kaldo
    Mar 13 '18 at 13:37
  • \$\begingroup\$ @Kaldo still, 15 bytes loses to yours ;) \$\endgroup\$ Mar 13 '18 at 14:56
  • 1
    \$\begingroup\$ Alternate 14-byter: 'Ðœ₁Ω.D.•zíΘ•» \$\endgroup\$ Mar 13 '18 at 16:08
5
\$\begingroup\$

APL (Dyalog), 22 bytes

1 byte saved thanks to @EriktheOutgolfer

'Goose'⍪⍨(?9)5⍴'Duck '

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Alternative: 'Goose'⊣⎕←⍣(?9)⊢'Duck' \$\endgroup\$ Mar 12 '18 at 17:08
5
\$\begingroup\$

Retina, 17 bytes

?\\K`duck
K`goose

Try it online!

Try it online!

Prints 1 or 2 ducks, with equal probability.

Explanation

?\\K`duck

Set the working string to duck and print it with a trailing linefeed (\). Then this is wrapped in another output stage, but this one has the random flag (?) applied to it, so it only prints with a probability of 50%.

K`goose

Replace the duck with goose, which is printed implicitly at the end of the program.

Here's a fun alternative which prints 1 duck with 50% probability, 2 ducks with 25%, 3 ducks with 12.5%...:

\K`duck
?+\G`
K`goose
\$\endgroup\$
5
\$\begingroup\$

Vim (script) on Linux, 46 43 bytes (49 46 with : at start of line)

.!date +0\%N\%s6
s/6.*/goose
s/\d/duck\r/g

Executed as vim -S filename or pasted into running vim with : before each line.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to PPCG! You should be able to use \d over [0-9]. Also, it looks like vim doesn't require the trailing newline, so this can be 45 bytes :) \$\endgroup\$
    – H.PWiz
    Mar 12 '18 at 21:19
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Steadybox
    Mar 12 '18 at 21:22
5
\$\begingroup\$

><>, 31 22 bytes

"duck"a>x
r"esoog"<>o<

Try it online!

-9 bytes based on Not a tree's revision

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You could remove l?!; and exit with an error. \$\endgroup\$
    – Emigna
    Mar 12 '18 at 20:59
  • 1
    \$\begingroup\$ And you could save some more like this \$\endgroup\$
    – Emigna
    Mar 12 '18 at 21:04
  • 1
    \$\begingroup\$ Or like this. \$\endgroup\$
    – Not a tree
    Mar 13 '18 at 1:38
  • 3
    \$\begingroup\$ @Notatree I get that one to sometimes print "\nkcud\nkcud...". It rarely behaves correctly \$\endgroup\$
    – Suppen
    Mar 13 '18 at 7:50
  • 1
    \$\begingroup\$ @Suppen Just using an arrow instead of a mirror fixes it; see edit. \$\endgroup\$
    – KSmarts
    Mar 13 '18 at 13:41
5
\$\begingroup\$

Befunge 98, 38 30 25 bytes

"esooG"v>:#,_@
"Duck"a<?<

Try it online!

  • Thanks @JoKing for stripping the useless trampoline
  • Switching to Befunge 98 for shorter new line - now Duck fits within a single string
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You don't need the # on the second line \$\endgroup\$
    – Jo King
    Mar 14 '18 at 2:11
  • \$\begingroup\$ And "technically", whitespace within a line is fine so you can cut out a couple of quotes (even if the output looks weird) \$\endgroup\$
    – Jo King
    Mar 14 '18 at 2:16
  • \$\begingroup\$ @JoKing Thanks, see my edit. \$\endgroup\$
    – Vincent
    Mar 14 '18 at 9:26
5
\$\begingroup\$

T-SQL, 70 44 43 bytes (Many ducks)

while rand()<.9print'duck'print'duck
goose'

Thanks @Zac Faragher!

Revised Version, 54 43 40 bytes (1 or 2 ducks)

Thanks @BradC!

if rand()<.5print'duck'print'duck
goose'


I can't seem to get this to run properly in SQL Fiddle, but it works just fine in LINQPad and SSMS.

Not sure if this is a known limitation of SQL Fiddle or I'm just doing something wrong

\$\endgroup\$
8
  • \$\begingroup\$ Nice. Down to 61 if you change select to print and replace the final 'duck'select'goose' with 'duck(linebreak)goose' (with a literal linebreak, of course) \$\endgroup\$
    – BradC
    Mar 13 '18 at 18:17
  • \$\begingroup\$ Actually, you can just do while rand()<.5print'duck'print'duck(linebreak)goose' for 43. \$\endgroup\$
    – BradC
    Mar 13 '18 at 18:29
  • \$\begingroup\$ Nice, thanks @BradC. Is there another way to represent a line break in a string other than adding a char(13)? \$\endgroup\$
    – Probably
    Mar 13 '18 at 18:51
  • \$\begingroup\$ Yep, just literally put a return inside the quotes, I'll edit your post to show you (comments don't show returns). \$\endgroup\$
    – BradC
    Mar 13 '18 at 19:33
  • 1
    \$\begingroup\$ Save 3 more bytes: change the first while to if. Prints (randomly) either one duck or two, which (I believe) still satisfies the challenge. \$\endgroup\$
    – BradC
    Mar 13 '18 at 20:02
5
\$\begingroup\$

Powershell - 31 30 bytes

Warning: You're most likely going to end up with a lot of ducks. Random includes the values of 0 to Int32.MaxValue so, depending on how random you're number is, this could be a lot of quacking.

1..(Random)|%{"Duck"};"Goose"
\$\endgroup\$
2
  • \$\begingroup\$ You shouldn't need the $ in front of (Random). Try it online! \$\endgroup\$ Mar 19 '18 at 13:07
  • \$\begingroup\$ @AdmBorkBork thanks for the catch! \$\endgroup\$ Mar 19 '18 at 13:44
4
\$\begingroup\$

Fission, 24 19 bytes

R"duck"N#
"goose"*[

Try it online!

-5 bytes thanks to Martin Ender

\$\endgroup\$
0
4
\$\begingroup\$

Brachylog, 21 bytes

1ṙ+₁;"Duck
"j₍,"Goose

Try it online!

Hey, language of the month going inactive, let's kick things up a little!

\$\endgroup\$
4
\$\begingroup\$

Befunge

57 35 bytes (perimeter of the entire field is 19x3 17x2 charachters) Thanks to karhell for the improvement.

55+"kcud",,,,,#v?
  @,,,,,"goose"<

The second line puts duck and a newline on the stack (backwards) and outputs it as a string. After that 75% chance of going back to the start and printing duck again, 25% (when question mark decides to go down) to print goose and stop.

\$\endgroup\$
2
  • \$\begingroup\$ If a 50% chance to goose is acceptable, 55+"kcud",,,,,#v?(linebreak here) @,,,,,"goose"< would cut your code down to 35 bytes \$\endgroup\$
    – karhell
    Mar 13 '18 at 15:54
  • \$\begingroup\$ Ah the sweet wrap-around. You're right, thanks, I'll post an update later! \$\endgroup\$
    – rael_kid
    Mar 14 '18 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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