21
\$\begingroup\$

Create a program that outputs itself.

However, if the source code is repeated n times (meaning to concatenate a copy of the source code to the end n-1 times), then there should be 1/n probability outputting the original source code, a 1/n probability of outputting the source code repeated twice, a 1/n probability of outputting the source code repeated three times, ..., and a 1/n probability of outputting the source code n times.

For example, if your program is foobar, then it should always output exactly foobar. However, if you run foobarfoobarfoobarfoobar, then there should be a 1⁄4 chance each of outputting foobar, foobarfoobar, foobarfoobarfoobar and foobarfoobarfoobarfoobar.

  • The distribution of each possible output should be equal
  • In addition to standard I/O methods applying and standard loopholes forbidden, standard quine rules apply (can't access its own source, etc.)
  • This is code golf so shortest answer in bytes wins
asked May 22, 2018 at 21:52
\$\endgroup\$
7
  • \$\begingroup\$ Is pseudorandom good enough? \$\endgroup\$ Commented May 22, 2018 at 22:10
  • 1
    \$\begingroup\$ @wastl Yep, that's alright \$\endgroup\$ Commented May 22, 2018 at 22:31
  • 2
    \$\begingroup\$ Your definition is slightly off. If you repeat it 1 time (i.e. foobarfoobar) then it'd only print foobar \$\endgroup\$ Commented May 23, 2018 at 2:34
  • 1
    \$\begingroup\$ @Veskah foobarfoobar is foobar repeated two times, not once. \$\endgroup\$ Commented May 23, 2018 at 8:25
  • \$\begingroup\$ @Nit You start with the source. If you use it again, you have only repeated it once but have two instances. \$\endgroup\$ Commented May 23, 2018 at 22:16

8 Answers 8

13
\$\begingroup\$

Perl 5, (削除) 82 (削除ここまで) 80 bytes

$s=q($s=q(%s);printf"$s\n+1#",$s for 0..rand);printf"$s\n+1#",$s for 0..rand
+1#

Try it online! or Test suite

answered May 22, 2018 at 22:23
\$\endgroup\$
9
\$\begingroup\$

Jelly, (削除) 24 (削除ここまで) 22 bytes

"Ṿ;ẋŻɼLX¤¤μ"Ṿ;ẋŻɼLX¤¤μ

Try it online!

Try it x4!

"Ṿ;ẋŻɼLX¤¤μ"Ṿ;ẋŻɼLX¤¤μ
"Ṿ;ẋŻɼLX¤¤μ" String literal: Ṿ;ẋŻɼLX¤¤μ
 Ṿ Uneval. Return "Ṿ;ẋŻɼLX¤¤μ" (with quotes)
 ; Append the initial string. Yields the source code. 
 ɼ Apply the preceding link to the register and store the 
 result in the register.
 Ż Prepend 0. 
 Each time Żɼ is used when the source code is repeated
 the register's length increases by one. 
 We can't use ‘ because it closes string literals.
 L Length. Returns the number of times the source code has
 been repeated up till now.
 X Random element. If ɼ results in n, X returns a random 
 integer between 1 and n.
 ¤ Combines ŻɼLX as a nilad. 
 ẋ Repeat the source code a random number of times between
 1 and n.
 ¤ Close everything from the initial string literal as a
 nilad. 
 μ Start a new monadic chain. The register gets updated
 once for time the code is repeated but only the final
 repetition will result in output.
answered May 22, 2018 at 23:17
\$\endgroup\$
0
5
\$\begingroup\$

05AB1E, 32 bytes

"×ばつΩ"×ばつΩ

Try it online!

Explanation

"×ばつΩ" # push this string
 34ç # push a "-character
 ì # prepend it to the string
 DJ # duplicate and join the copy to the string
 1⁄4 # increment the counter
 32ô # split the string into pieces of size 32
 н # take the first one
 ×ばつ # repeat it for each in [1 ... counter]
 Ω # pick one at random
answered May 23, 2018 at 8:27
\$\endgroup\$
2
  • \$\begingroup\$ Could you add an explanation when you have a chance? \$\endgroup\$ Commented May 24, 2018 at 22:09
  • \$\begingroup\$ @Shaggy: Thanks for reminding me :) \$\endgroup\$ Commented May 25, 2018 at 5:58
2
\$\begingroup\$

Gol><>, 21 bytes

:QoaonC|P\Sx*F2ssS"
0

Try it online!

Explanation:

:Q | If the top of stack is 0, skip to next |
 Top of stack is implicitly 0
 P Increment top of stack
 \ Redirect pointer down]
0:QoaonC|P\Sx*F2ssS" Increment for each copy of the source code
0:QoaonC|P\Sx*F2ssS"
 \ Redirect back to the right
 Sx* Multiply the number of copies of the source code by a random number between 0 and 1
 F Repeat that many times
 2ss Push a double quote
 S" Print the source code minus the quote, newline and 0
 :Q If top of stack is not 0
 oaonC Print the quote, a newline and a 0 to complete the source code and continue the loop
 \Sx* Error on the * for some reason
answered May 26, 2018 at 10:41
\$\endgroup\$
1
\$\begingroup\$

Alice, 35 bytes

"d3a*h-&;adddd12h&}Uh*t&w.odt,k@
 !

Try it online!

Explanation

"

Like in many quines in 2D languages, this starts with a " that wraps around to itself and pushes the entire first line except the " itself.

d3a*h-&;

Adding one or more additional copies of the source code will place some implicit spaces at the end of the string literal. To make this actually a quine, we truncate the stack at 31 characters.

addd

Push a newline, then the stack height three times. The values pushed as the stack height are 32 (the space in the second line), 33 (the ! in the second line), and 34 (the initial ").

d

Push the stack height again, this time as the length of the original source code (35).

1

Initialize a counter at 1. This will count the number of times the source code is repeated.

2h&}

Turn right three times in place (i.e., turn left). Each additional repetition of the source code will contribute an h in the same column as this }, thus incrementing the counter. When the IP returns to the }, turn right again to continue in the same direction.

Uh

Take a uniform random number from 0 to n-1, then add 1 to get the number of times to output the original source.

*t&w

Multiply by the previously pushed stack height (code length), then repeat the following that many times by pushing a return address that many times minus one.

.o

Output the top of the stack without destroying it.

dt,

Move the bottom stack entry to the top.

k@

Repeat, then terminate after the loop is finished.

answered May 27, 2018 at 3:54
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 62 bytes

(f=a=>b=>b?f(a+.5):`(f=${f})(1)`.repeat(1+Math.random()*a))(1)

Try it online!

answered May 27, 2018 at 12:30
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Either I'm very unlucky, or this can't print more than two copies of the original source code. \$\endgroup\$ Commented May 27, 2018 at 16:11
  • \$\begingroup\$ @Nitrodon Thanks, fixed \$\endgroup\$ Commented May 27, 2018 at 16:42
1
\$\begingroup\$

Charcoal, 58 bytes

≔ ́θ ́⎚ ×ばつ ́⊕ ́‽ ́L ́⊞ ́O ́υ ́ω ́+ ́⪫ ́+ ́ ́ ́≔ ́θ ́ ́ ́ ́ ×ばつ⊕‽L⊞Oυω+⪫+ ́≔θ ́ ́θ

Try it online! No verbose version because the deverbosifier currently chokes on " ́". Mostly based on the Charcoal quine from Golf you a quine for great good!. Explanation:

≔ ́θ ́⎚ ×ばつ ́⊕ ́‽ ́L ́⊞ ́O ́υ ́ω ́+ ́⪫ ́+ ́ ́ ́≔ ́θ ́ ́ ́ ́ ́θθ

Assign the literal string ×ばつ⊕‽L⊞Oυω+⪫+ ́≔θ ́ ́θ to θ.

Clear any previous output, so that only the last output takes effect.

×ばつ⊕‽L⊞Oυω

Push the empty string to the predefined array. This makes the array length equal to the number of repetitions processed so far, so take its length, take a random number the implicit exclusive range, add 1, and repeat the following string that many times.

+⪫+ ́≔θ ́ ́θ

Prepend the literal string to θ, then insert literal ́s between each character, then suffix another copy of θ.

answered Jun 1, 2018 at 23:52
\$\endgroup\$
0
\$\begingroup\$

Python 3.8 (pre-release), (削除) 118 (削除ここまで) 116 bytes

for i in(1,c:=0):
 exec(s:="from random import*;c+=i or print('for i in(1,c:=0):\\n exec(s:=%r)#'%s*randint(1,c))")#

Try it online!

answered Oct 12, 2021 at 9:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.