# I multiply the source, you (probably) multiply the output!

The task is to write a program that outputs a consistent but otherwise arbitrary positive integer $$\x\$$ (so strictly greater than 0). Here's the catch: when the source is repeated $$\N\$$ times (the code is appended/concatenated $$\N-1\$$ to itself), the program should have a $$\\dfrac{1}{N}\$$ probability of outputting $$\N\cdot x\$$ and the remaining probability of $$\\dfrac{N-1}{N}\$$ of outputting $$\x\$$ unchanged.

## Example

Let's assume that your initial source is XYZ and produces the integer 3. Then:

• For $$\N=2\$$: XYZXYZ should output $$\3\$$ with a probability of $$\\frac{1}{2}\$$ (50% of the time) and $$\2\cdot 3=6\$$ with a probability of $$\\frac{1}{2}\$$ as well (50% of the time).

• For $$\N=3\$$: XYZXYZXYZ should output $$\3\$$ with a probability of $$\\frac{2}{3}\$$ (66.666% of the time) and $$\3\cdot 3=9\$$ with a probability of $$\\frac{1}{3}\$$ (33.333% of the time)

• For $$\N=4\$$: XYZXYZXYZXYZ should output $$\3\$$ with a probability of $$\\frac{3}{4}\$$ (75% of the time) and $$\4\cdot 3=12\$$ with a probability of $$\\frac{1}{4}\$$ (25% of the time)

and so on....

## Rules

• You must build a full program. The output has to be printed to STDOUT.

• Your program, should, in theory, output each possible value with the probabilities stated above, but a slight deviation from this due to the implementation of random is fine (provided that the implementation is not of a different distribution - you can't use a normal distribution to save bytes).

• The program should (again, in theory) work for an arbitrarily big value of $$\N\$$, but technical limitations due to precision are fine for large $$\N\$$.

• The output must be in base 10 (outputting in any other base or with scientific notation is forbidden). Trailing / leading spaces and leading zeroes are allowed.

• The initial source must (of course) be at least 1 byte long. You may not assume a newline between copies of your source. The program should not take input (or have an unused, empty input).

• This is , so an answer's score is the length of the (original) source in bytes, with a lower score being better.

Note: This challenge is a (much) harder version of this one.

• Can the program read its source code? – my pronoun is monicareinstate Sep 6 at 14:44
• @someone Yes, it is allowed. – Mr. Xcoder Sep 6 at 14:49

# R, 66 35 bytes

-29 bytes thanks to digEmAll.

-2 bytes thanks to Giuseppe.

+0->A
x=!0:F
F=F+1
sample(F*x+!x,1)


Try it online!

Check the distribution for N=4.

The key is the rightwards assignment ->. When the code is multiplied $$\N\$$ times, the first $$\N-1\$$ calls to sample will be assigned to A, and only the last call will be printed.

Original, more convoluted solution:

# R, 66 bytes

T->TT
F=F+1
TT=0
?=function(x)if(x)sample(c(1,F),1,,c(1,F-1))
?T


Try it online!

Try it online (repeated 3 times)!

Uses two tricks: 1) call the main function of interest ?, so that we can call it without ending the program with a bracket, and 2) use variables T and TT, with code which starts with T and ends with ?T.

F is the iteration counter. ? is redefined as a function which takes a boolean argument: if the input of ? is TRUE (or T), it does the required random sampling; if the input is FALSE (or 0), it does nothing. The value of TT is defined as 0, so that ?T does the sampling but ?TT does nothing.

When the source is repeated, it looks like this:

T->TT
F=F+1
TT=0
?=function(x)if(x)sample(c(1,F),1,,c(1,F-1))
?TT->TT
F=F+1
TT=0
?=function(x)if(x)sample(c(1,F),1,,c(1,F-1))
?T


so the middle call ?TT outputs nothing but the final call ?T outputs the random result.

• I don't think I've ever seen -> used in code golf in a situation where <- couldn't be; this is so cool!! – Giuseppe Sep 6 at 23:45
• PS I'm gonna give this a bounty at some point. – Giuseppe Sep 7 at 2:13
• Absolutely awesome! – digEmAll Sep 7 at 10:17
• This should work as well I think (borrowing +0 idea from python) – digEmAll Sep 7 at 15:35
• @digEmAll Much neater, thanks! – Robin Ryder Sep 7 at 15:59

# Python 3, 81 79 bytes

+0if[]else 1
from random import*
try:n+=1
except:n=1
print([1,n][random()*n<1])


Try it online!

-1 byte thanks to @Nishioka

This is one Python 3 solution that does not access the program source directly. Doing this in Python 3 is more challenging than Python 2 because normal printing statements end with a closing parenthesis so there aren't many choices to change its behavior in the next block of initial source. It would be interesting to see more creative solutions in Python 3.

• -1 byte: +0 if[]else 1 – Nishioka Sep 7 at 19:49
• @Nishioka Thanks. Updated. – Joel Sep 7 at 20:45

# Bash, 31 bytes

trap echo\ $[RANDOM%++n?1:n] 0;  Try it online! trap ... 0 will run the code contained on exit. Repeated traps will overwrite old ones. The unquoted $[arithmetic expansion] gets run every time a new trap is set.

Zsh can save one byte with <<<:

trap "<<<$[RANDOM%++n?1:n]" 0;  # Jelly, 7 bytes ‘ɼḷXỊ®*  Try it online! # 05AB1E, 7 bytes ¾Å1¼¾ªΩ  Try it online! # Python 3, 7876 75 bytes Using the same trick as in the link that was posted, here is a Python one (with x=1). from random import*;n=len(*open(__file__))//75;print(1+~-n*(random()<1/n))#  Try it online! -2 bytes thanks to Mr. Xcoder for his (n-1) formula with ~-n which has higher precedence than * -1 byte thanks to Nishioka • Looks good to me! import random;n=len(*open(__file__))//76;print(1+~-n*(random.random()<1/n))# should work for -2 bytes – Mr. Xcoder Sep 6 at 14:22 • I'd never seen this way of doing n-1 ! I like it, thanks :) – Pâris Douady Sep 6 at 14:25 • -1 byte: tio.run/##K6gsycjPM/7/… – Nishioka Sep 6 at 15:30 • Another -1 byte but with a bit different approach: tio.run/##K6gsycjPM/7/… – Nishioka Sep 6 at 17:13 • yes I do because of the random()<1/n ;-) – Pâris Douady Sep 7 at 18:48 # Gaia, 171514 13 bytes Øgl13÷:(1w&+ṛ  Try it online! I randomly noticed the behavior of Øg yesterday when looking through the docs, which helped immensely. # Perl 5, 28 26 bytes -2 bytes thanks to @Grimy 1 if!++$x;say 1<rand$x||$x


TIO

• 26: 1 if!++$x;say 1<rand$x||$x – Grimmy Sep 7 at 3:11 • thanks, nice variation – Nahuel Fouilleul Sep 7 at 17:36 # Ruby, 40 bytes 1 n||=@x=0 n+=1 puts 1>rand(n)?n:1 if @x  Try it online! Try it online (copied 3 times)! A ruby port of this Python answer. # Ruby, 38 bytes 2 bytes saved by reading the file: n=File.size($0)/38;puts 1>rand(n)?n:1#


Try it online!

# Dyalog APL, 25242322 21 bytes

0{×⍺:1+⍵⋄⎕←1⌈⍵×1=?⍵}1


Try it online!

# Runic Enchantments, 31 bytes

UwR'10<;$\ I+:'RA0)?/1$;
1
l;
y


Try it online!

Uses the same structure as this answer to count how many times the source has been duplicated:

Just instead of outputting the nth number in a list, we use that value to randomly generate a number, if the result is not 0, print 1, else print that number.

# Japt, 9 8 bytes

(°Tö)ÎªT

(°Tö)ÎªT
(            :Prevent the operator that follows from being implicitly applied to the first input variable, U
°T          :Increment T (initially 0) by 1
ö         :Random element in the range [0,T)
)        :Closing the parentheses here instead of after the T saves a byte as there would need to be a space here to close the random method
Î       :Sign - 0 (falsey) or 1 (truthy)
ªT     :Logical OR with current value of T


## Original, 131110 9 bytes

Note the trailing space.

NoÎp°T ö

NoÎp°T ö
N             :Initially, the (empty) array of inputs
o            :Replace the last element with
Î           :  Its sign (always 1)
p          :Push
°T        :  T (initially 0) incremented
ö      :Random element of N


# JavaScript (JavaScript shell 71), 78 bytes

(async x=>x)().then(x=>f((''+f).length/78));f=x=>print(1-~x*Math.random()|0)//


No tio link, spidermonkey on tio is too old...

Firefox (Spidermonkey) consider the comment as a part of function f. As the result, (''+f).length will be b+79n where b < 78, and (n + 1) is the times of source code repeated.

This buggy (? I'm not sure. I would prefer it is a bug of JavaScript specification rather than any interpreter) behavior had been submit to BMO by someone else just after this answer posted: https://bugzilla.mozilla.org/show_bug.cgi?id=1579792 . (Neither of the bmo thread nor the tweet is posted by me.)

• What's with the (async x=>x)()? Why is it async? – Tomáš Zato - Reinstate Monica Sep 9 at 9:46
• @TomášZato It is literally asynchronous. So the callback x=>f(...) will be invoked after function f been defined. – tsh Sep 10 at 2:32

# C# (Visual C# Interactive Compiler), 133114 112 bytes

This is the first (and hopefully last) time I have ever used C# preprocessor directives.

#if!I
#define I
static int i;
class p{~p()=>Console.Write(new Random().Next(i)<1?i:1);}p s=new p();
#endif
i++;



Try it online!

# Charcoal, 12 bytes

⎚Ｉ⎇‽Ｌ⊞Ｏυω¹Ｌυ


Try it online! Based on my answer to the linked question. Outputs n with probability ¹/ₙ, otherwise 1. Explanation:

⎚               Remove output from previous iterations
υ        Initially empty list
ω       Empty string
⊞Ｏ         Push
Ｌ           Length
‽            Random integer [0..length)
⎇             Ternary
¹      If nonzero then literal 1
Ｌυ    If zero then the new length
Ｉ              Cast to string for implicit print