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.
-
11\$\begingroup\$ Could you define indeterminate? Could it mean either zero or one? \$\endgroup\$recursive– recursive2018年03月12日 15:51:14 +00:00Commented Mar 12, 2018 at 15:51
-
1\$\begingroup\$ Please define how randomly this should be generated. Uniform in range or with exponential decline? \$\endgroup\$hyperneutrino– hyperneutrino ♦2018年03月12日 15:52:43 +00:00Commented Mar 12, 2018 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– AJFaraday2018年03月12日 15:52:55 +00:00Commented Mar 12, 2018 at 15:52
-
3\$\begingroup\$ Speaking as a Minnesotan, what if mine prints "duck, duck, gray duck" instead? :) \$\endgroup\$Mike Hill– Mike Hill2018年03月14日 19:13:59 +00:00Commented Mar 14, 2018 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– AJFaraday2018年03月15日 23:13:08 +00:00Commented Mar 15, 2018 at 23:13
125 Answers 125
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]}
-
4\$\begingroup\$ It always makes me glad when someone uses this on PCG. \$\endgroup\$Etheryte– Etheryte2018年03月13日 20:03:08 +00:00Commented Mar 13, 2018 at 20:03
-
\$\begingroup\$ Well i forgot this game existed. \$\endgroup\$Caimen– Caimen2018年03月16日 19:33:50 +00:00Commented Mar 16, 2018 at 19:33
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")
-
11\$\begingroup\$ Oh my lord... Why have I never thought of this. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年03月13日 19:03:48 +00:00Commented Mar 13, 2018 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\$Ander Biguri– Ander Biguri2018年03月14日 10:59:25 +00:00Commented Mar 14, 2018 at 10:59
-
2\$\begingroup\$ @AnderBiguri I suspect the Minecraft one would do a little worse than 81 bytes (blocks?) ;) \$\endgroup\$Chris– Chris2018年03月14日 19:24:03 +00:00Commented Mar 14, 2018 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– gastropner2018年03月15日 10:33:04 +00:00Commented Mar 15, 2018 at 10:33
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 ddgin 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
Edit: Changed {"text":"duck"} to just "duck" (and the same with "goose")
-
1\$\begingroup\$ Beautiful. We need more Minecraft in PPCG. \$\endgroup\$BradC– BradC2018年03月15日 19:23:32 +00:00Commented Mar 15, 2018 at 19:23
-
10\$\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– BradC2018年03月15日 21:56:32 +00:00Commented Mar 15, 2018 at 21:56
-
1\$\begingroup\$ @BradC there should be entire challenges based around Minecraft. \$\endgroup\$tox123– tox1232018年03月18日 17:47:33 +00:00Commented Mar 18, 2018 at 17:47
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>
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:
-
1\$\begingroup\$ This has totally made my day! \$\endgroup\$AJFaraday– AJFaraday2018年03月23日 08:46:56 +00:00Commented Mar 23, 2018 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– lucasb2018年03月23日 16:47:39 +00:00Commented Mar 23, 2018 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– AJFaraday2018年03月23日 16:48:39 +00:00Commented Mar 23, 2018 at 16:48
-
\$\begingroup\$ Isn't this a standard loophole, to get external resources? \$\endgroup\$MilkyWay90– MilkyWay902019年01月12日 16:56:03 +00:00Commented Jan 12, 2019 at 16:56
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'])
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.
-
\$\begingroup\$
disp(['duck '+~pwd';'goose'])? Not sure if this is "indeterminate" enough \$\endgroup\$Luis Mendo– Luis Mendo2018年03月12日 19:53:56 +00:00Commented Mar 12, 2018 at 19:53
Python 2, (削除) 36 (削除ここまで) 34 bytes
print"duck\n"*((id(id)%5)+1),"goose"
Suggestion by Kevin Cruijssen gets us to 34 bytes:
print"duck\n"*-~(id(id)%5),"goose"
-
3\$\begingroup\$ Welcome to the site, and nice first answer! \$\endgroup\$2018年03月12日 20:30:46 +00:00Commented Mar 12, 2018 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\$Kevin Cruijssen– Kevin Cruijssen2018年03月13日 11:50:27 +00:00Commented Mar 13, 2018 at 11:50 -
1\$\begingroup\$ Can
id(id)not beid(0)or am I missing something? \$\endgroup\$Jonathan Frech– Jonathan Frech2018年03月14日 11:18:31 +00:00Commented Mar 14, 2018 at 11:18 -
\$\begingroup\$ @Johnathan, looking at
id(0)on a couple different computers suggests it's a constant. \$\endgroup\$user2699– user26992018年03月14日 18:21:40 +00:00Commented Mar 14, 2018 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– jpmc262018年03月15日 23:17:19 +00:00Commented Mar 15, 2018 at 23:17
Perl 5, 20 bytes
First a practical 26 bytes:
Ducks 1 to 9 times before being goosed.
say"Duck
"x(1+$^T%9),Goose
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.
-
\$\begingroup\$ If you change
1+$^T%9to just$^T, it still works, and you save 4 bytes. \$\endgroup\$Chris– Chris2018年03月13日 01:47:44 +00:00Commented Mar 13, 2018 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– Ton Hospel2018年03月13日 06:03:18 +00:00Commented Mar 13, 2018 at 6:03
Bash, (削除) 39 (削除ここまで) (削除) 38 (削除ここまで) 37 bytes
sed s/[0-9]/duck\\n/g<<<$RANDOM\goose
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.
-
1\$\begingroup\$ Welcome to PPCG! \$\endgroup\$Steadybox– Steadybox2018年03月12日 19:31:28 +00:00Commented Mar 12, 2018 at 19:31
-
1\$\begingroup\$ You can save one byte by getting rid of the single quotes as long as you replace
\nwith\\n. \$\endgroup\$Chris– Chris2018年03月13日 01:55:32 +00:00Commented Mar 13, 2018 at 1:55 -
1\$\begingroup\$ Nice idea. You can do something similar with just bash constructs and no sed for 30 bytes. \$\endgroup\$Digital Trauma– Digital Trauma2018年03月13日 06:27:17 +00:00Commented Mar 13, 2018 at 6:27
-
1\$\begingroup\$
sed s/[0-9]/duck\\n/g<<<$RANDOM\goosecan shave off one byte \$\endgroup\$sch– sch2018年03月13日 16:49:24 +00:00Commented Mar 13, 2018 at 16:49 -
1\$\begingroup\$ The
sedisn'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– tripleee2018年03月13日 19:33:38 +00:00Commented Mar 13, 2018 at 19:33
R, 35 bytes
cat(rep("duck
",rexp(1)+1),"goose")
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.
-
1\$\begingroup\$ The probability of outputting more than one duck is
exp(-1)or around 36.8%. \$\endgroup\$Giuseppe– Giuseppe2018年03月12日 16:50:32 +00:00Commented Mar 12, 2018 at 16:50 -
\$\begingroup\$ It asks to print it on each line. I think you should add " \n" to your "duck" \$\endgroup\$Leonhard Euler– Leonhard Euler2018年03月13日 10:54:22 +00:00Commented Mar 13, 2018 at 10:54
-
3\$\begingroup\$ @stuartstevenson Here I have used a literal newline rather than
\nas it is one byte shorter. If you visit the "Try it online!" link you can see that the effect is the same. \$\endgroup\$user2390246– user23902462018年03月13日 11:02:30 +00:00Commented Mar 13, 2018 at 11:02 -
\$\begingroup\$ On a DOS/Windows box, that newline would still be two (2) bytes. 0x0d0a \$\endgroup\$lit– lit2018年03月17日 19:16:21 +00:00Commented Mar 17, 2018 at 19:16
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
-
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
RANDOMto$to shave 5 bytes?And one could also do a very long string of ducks:yes duck|head -n $$;echo goose\$\endgroup\$Olivier Dulac– Olivier Dulac2018年03月14日 16:28:43 +00:00Commented Mar 14, 2018 at 16:28 -
\$\begingroup\$ @OlivierDulac yes, I think using
$$is acceptable - thanks! \$\endgroup\$Digital Trauma– Digital Trauma2018年03月14日 17:03:11 +00:00Commented Mar 14, 2018 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\$Olivier Dulac– Olivier Dulac2018年03月14日 17:29:58 +00:00Commented Mar 14, 2018 at 17:29
Jelly, 13 bytes
2X"¢;ÆS»ẋ"ʋ¢»
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.
-
\$\begingroup\$ Congrats! I have literally no idea how this works! \$\endgroup\$AJFaraday– AJFaraday2018年03月16日 10:19:18 +00:00Commented Mar 16, 2018 at 10:19
-
\$\begingroup\$ @AJFaraday I went ahead and added an explanation for you \$\endgroup\$PunPun1000– PunPun10002018年03月16日 13:07:59 +00:00Commented Mar 16, 2018 at 13:07
-
\$\begingroup\$ @PunPun1000 Sorry, but what's the stack? Jelly is a tacit programming language. \$\endgroup\$user202729– user2027292018年03月16日 13:51:22 +00:00Commented Mar 16, 2018 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– PunPun10002018年03月16日 14:02:08 +00:00Commented Mar 16, 2018 at 14:02
-
\$\begingroup\$ There's an implicit concatenation in Jelly? Yay! \$\endgroup\$MilkyWay90– MilkyWay902019年03月21日 23:38:22 +00:00Commented Mar 21, 2019 at 23:38
-
\$\begingroup\$ I’m not sure it’s a cheat, strictly speaking. \$\endgroup\$AJFaraday– AJFaraday2018年03月14日 06:44:14 +00:00Commented Mar 14, 2018 at 6:44
-
\$\begingroup\$ maybe you could do
*(1/rand)to shed 2 bytes. this could fail in the caserandreturns0.0, but that seems astronomically unlikely. or well, i tried100_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– epidemian2021年05月28日 15:54:47 +00:00Commented May 28, 2021 at 15:54 -
\$\begingroup\$ @epidemian Answers with extremely small chances of malfunctioning are not allowed \$\endgroup\$The Fifth Marshal– The Fifth Marshal2021年06月07日 23:02:19 +00:00Commented Jun 7, 2021 at 23:02
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}'
a is the append command in sed, and q quits. Both only run on the line $$, which corresponds to the PID.
-
\$\begingroup\$ Bash has modulo, though that will increase the size. Try
head -n $((1+(RANDOM % 5)))\$\endgroup\$tripleee– tripleee2018年03月13日 20:12:49 +00:00Commented Mar 13, 2018 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– Chris2018年03月13日 20:16:58 +00:00Commented Mar 13, 2018 at 20:16
-
1
-
\$\begingroup\$ @OlivierDulac suggested using
$$instead of$RANDOMfor my answer. I think you could use the same to save you 5 bytes. Oh and trysedtoo:yes duck|sed 1$$q;echo goose\$\endgroup\$Digital Trauma– Digital Trauma2018年03月14日 17:10:44 +00:00Commented Mar 14, 2018 at 17:10 -
\$\begingroup\$ In fact
yes duck|sed $$q;echo gooseis fine - I don't think your script would ever get a PID < 1. \$\endgroup\$Digital Trauma– Digital Trauma2018年03月14日 17:17:06 +00:00Commented Mar 14, 2018 at 17:17
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" ...
-
\$\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– AJFaraday2018年03月12日 16:12:20 +00:00Commented Mar 12, 2018 at 16:12
-
1\$\begingroup\$ @AdmBorkBork, this Minnesotan joined this group to just +1 your gray duck. \$\endgroup\$Milwrdfan– Milwrdfan2018年03月12日 17:06:29 +00:00Commented Mar 12, 2018 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. (And0..0does print a duck) \$\endgroup\$TessellatingHeckler– TessellatingHeckler2018年03月16日 08:17:12 +00:00Commented Mar 16, 2018 at 8:17 -
\$\begingroup\$ @TessellatingHeckler Thanks, but I just-now came up with a 28-byte version. :) \$\endgroup\$AdmBorkBork– AdmBorkBork2018年03月16日 12:32:51 +00:00Commented Mar 16, 2018 at 12:32
C# (Visual C# Interactive Compiler), 55 bytes
Write("{"+new Random().Next(2)+"}{0}goose","duck\n","")
-
\$\begingroup\$ Really clever use of string formatting! \$\endgroup\$Gymhgy– Gymhgy2019年04月30日 02:46:08 +00:00Commented Apr 30, 2019 at 2:46
Brachylog, (削除) 24 (削除ここまで) 21 bytes
-3 bytes thanks to Erik the Outgolfer
"duck"ẉ4ṙ0∧"goose"w∨↰
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
-
\$\begingroup\$ So, if I’m reading this right, it reads from left-to-right, bottom-to-top? \$\endgroup\$AJFaraday– AJFaraday2018年03月13日 21:33:35 +00:00Commented Mar 13, 2018 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– PunPun10002018年03月13日 21:36:31 +00:00Commented Mar 13, 2018 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– AJFaraday2018年03月13日 21:49:53 +00:00Commented Mar 13, 2018 at 21:49
-
\$\begingroup\$ You can merge the two predicates like this and tie me. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年03月13日 23:10:32 +00:00Commented Mar 13, 2018 at 23:10
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.
Goose and 2nd duck don't have a Group ID enter image description here
Inside the random trigger. enter image description here
-
\$\begingroup\$ I sense a theme for this challenge \$\endgroup\$Benjamin Urquhart– Benjamin Urquhart2019年03月18日 23:09:59 +00:00Commented Mar 18, 2019 at 23:09
-
\$\begingroup\$ @BenjaminUrquhart What? \$\endgroup\$MilkyWay90– MilkyWay902019年03月18日 23:10:51 +00:00Commented Mar 18, 2019 at 23:10
-
\$\begingroup\$ There are submissions for Word of Warcraft, Minecraft, and some other game as well. \$\endgroup\$Benjamin Urquhart– Benjamin Urquhart2019年03月18日 23:11:28 +00:00Commented Mar 18, 2019 at 23:11
-
2\$\begingroup\$ @BenjaminUrquhart Better find more games to answer in \$\endgroup\$MilkyWay90– MilkyWay902019年03月18日 23:12:06 +00:00Commented Mar 18, 2019 at 23:12
05AB1E, (削除) 15 (削除ここまで) 14 bytes
'М1Ωи`.•zíΘ•»
Will print 2, 5 or 6 ducks and then goose.
-1 byte thanks to @Emigna using ' for a single compressed word (duck)
-
1\$\begingroup\$ You can use
'Мsince duck is a single word. \$\endgroup\$Emigna– Emigna2018年03月12日 16:39:20 +00:00Commented Mar 12, 2018 at 16:39 -
1\$\begingroup\$ Beats mine:
['МTΩ#].•zíΘ•»\$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年03月13日 13:20:41 +00:00Commented Mar 13, 2018 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\$user2956892– user29568922018年03月13日 13:37:05 +00:00Commented Mar 13, 2018 at 13:37
-
\$\begingroup\$ @Kaldo still, 15 bytes loses to yours ;) \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年03月13日 14:56:31 +00:00Commented Mar 13, 2018 at 14:56
-
1\$\begingroup\$ Alternate 14-byter:
'М1Ω.D.•zíΘ•»\$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年03月13日 16:08:04 +00:00Commented Mar 13, 2018 at 16:08
-
\$\begingroup\$ Alternative:
'Goose'⊣⎕←⍣(?9)⊢'Duck'\$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年03月12日 17:08:41 +00:00Commented Mar 12, 2018 at 17:08
Retina, 17 bytes
?\\K`duck
K`goose
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
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.
-
2\$\begingroup\$ Welcome to PPCG! You should be able to use
\dover[0-9]. Also, it looks like vim doesn't require the trailing newline, so this can be 45 bytes :) \$\endgroup\$H.PWiz– H.PWiz2018年03月12日 21:19:36 +00:00Commented Mar 12, 2018 at 21:19 -
\$\begingroup\$ Welcome to PPCG! \$\endgroup\$Steadybox– Steadybox2018年03月12日 21:22:14 +00:00Commented Mar 12, 2018 at 21:22
-
1\$\begingroup\$ You could remove
l?!;and exit with an error. \$\endgroup\$Emigna– Emigna2018年03月12日 20:59:07 +00:00Commented Mar 12, 2018 at 20:59 -
1
-
1\$\begingroup\$ Or like this. \$\endgroup\$Not a tree– Not a tree2018年03月13日 01:38:04 +00:00Commented Mar 13, 2018 at 1:38
-
3\$\begingroup\$ @Notatree I get that one to sometimes print "\nkcud\nkcud...". It rarely behaves correctly \$\endgroup\$Suppen– Suppen2018年03月13日 07:50:34 +00:00Commented Mar 13, 2018 at 7:50
-
1\$\begingroup\$ @Suppen Just using an arrow instead of a mirror fixes it; see edit. \$\endgroup\$KSmarts– KSmarts2018年03月13日 13:41:24 +00:00Commented Mar 13, 2018 at 13:41
Befunge 98, (削除) 38 (削除ここまで) (削除) 30 (削除ここまで) 25 bytes
"esooG"v>:#,_@
"Duck"a<?<
- Thanks @JoKing for stripping the useless trampoline
- Switching to Befunge 98 for shorter new line - now
Duckfits within a single string
-
1\$\begingroup\$ You don't need the
#on the second line \$\endgroup\$Jo King– Jo King2018年03月14日 02:11:05 +00:00Commented Mar 14, 2018 at 2:11 -
\$\begingroup\$ And "technically",
whitespace within a line is fineso you can cut out a couple of quotes (even if the output looks weird) \$\endgroup\$Jo King– Jo King2018年03月14日 02:16:43 +00:00Commented Mar 14, 2018 at 2:16 -
\$\begingroup\$ @JoKing Thanks, see my edit. \$\endgroup\$Vincent– Vincent2018年03月14日 09:26:35 +00:00Commented Mar 14, 2018 at 9:26
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
-
\$\begingroup\$ Nice. Down to 61 if you change
selecttoprintand replace the final'duck'select'goose'with'duck(linebreak)goose'(with a literal linebreak, of course) \$\endgroup\$BradC– BradC2018年03月13日 18:17:40 +00:00Commented Mar 13, 2018 at 18:17 -
\$\begingroup\$ Actually, you can just do
while rand()<.5print'duck'print'duck(linebreak)goose'for 43. \$\endgroup\$BradC– BradC2018年03月13日 18:29:16 +00:00Commented Mar 13, 2018 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– Probably2018年03月13日 18:51:50 +00:00Commented Mar 13, 2018 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– BradC2018年03月13日 19:33:59 +00:00Commented Mar 13, 2018 at 19:33
-
1\$\begingroup\$ Save 3 more bytes: change the first
whiletoif. Prints (randomly) either one duck or two, which (I believe) still satisfies the challenge. \$\endgroup\$BradC– BradC2018年03月13日 20:02:24 +00:00Commented Mar 13, 2018 at 20:02
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"
-
\$\begingroup\$ You shouldn't need the
$in front of(Random). Try it online! \$\endgroup\$AdmBorkBork– AdmBorkBork2018年03月19日 13:07:54 +00:00Commented Mar 19, 2018 at 13:07 -
\$\begingroup\$ @AdmBorkBork thanks for the catch! \$\endgroup\$SomeShinyObject– SomeShinyObject2018年03月19日 13:44:38 +00:00Commented Mar 19, 2018 at 13:44
Brachylog, 21 bytes
1ṙ+1;"Duck
"j(,"Goose
Hey, language of the month going inactive, let's kick things up a little!
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