[Question]
  [
## Task
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*)](https://codegolf.meta.stackexchange.com/a/10923).
* 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 [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), 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](https://codegolf.stackexchange.com/questions/132558/i-double-the-source-you-double-the-output).
  
[Answer]
# [R](https://www.r-project.org/), ~~66~~ 35 bytes
-29 bytes thanks to [digEmAll](https://codegolf.stackexchange.com/users/41725/digemall).
-2 bytes thanks to [Giuseppe](https://codegolf.stackexchange.com/users/67312/giuseppe).
```
+0->A
x=!0:F
F=F+1
sample(F*x+!x,1)
```
[Try it online!](https://tio.run/##K/r/X9tA186Rq8JW0cDKjcvN1k3bkKs4MbcgJ1XDTatCW7FCx1Dz/38A "R – Try It Online")
[Check the distribution for N=4.](https://tio.run/##K/r/X9tA186Rq8JW0cDKjcvN1k3bkKs4MbcgJ1XDTatCW7FCx1CTbkr@lyQmAflFqQU5mcmJJakahgZAoIOuTFPzPwA "R – Try It Online")
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](https://www.r-project.org/), 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!](https://tio.run/##K/r/P0TXLiSEy83WTduQKyTE1oArwT7BNq00L7kkMz9Po0IzMw1IFCfmFuSkaiRrGOq4aeoY6uiAWbqGmppc9iH//wMA "R – Try It Online")
[Try it online (repeated 3 times)!](https://tio.run/##K/r/P0TXLiSEy83WTduQKyTE1oArwT7BNq00L7kkMz9Po0IzMw1IFCfmFuSkaiRrGOq4aeoY6uiAWbqGmppc9iGDwYT//wE "R – Try It Online")
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.
[Answer]
# [Python 3](https://docs.python.org/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!](https://tio.run/##K6gsycjPM/7/X9sgMy06NjWnOFXBkCutKD9XoSgxLwVIZeYW5BeVaHGVFFVa5WnbGnKlViSnFpRY5QGZBUWZeSUa0YY6ebHREOUamlp5Noaxmv//AwA "Python 3 – 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.
[Answer]
# [Bash](https://www.gnu.org/software/bash/), 31 bytes
```
trap echo\ $[RANDOM%++n?1:n] 0;
```
[Try it online!](https://tio.run/##VY49D4IwGIT3/ooLgXQwEDAuAsGYqJuSuIoDX4UuLSllUn97LV2MN72X9@7JNfU8mrbWKDAgz0HP5YUareoJfTvKCv7jfrydymuw2YhDkoon4szYENFqEbbYI5wRJhgIkwocXOCVRNH@k6GTBFYrfUBRgDk7KS40Ay0XPS16hmSwJAS7LgWF53PPxf5o2/iHW9XY1WB4QyvQSlBbpO7ZSdG7Yx1PnDNf "Bash – Try It Online")
`trap ... 0` will run the code contained on exit. Repeated `trap`s will overwrite old ones. The unquoted `$[arithmetic expansion]` gets run every time a new trap is set.
---
[Zsh can save one byte with `<<<`:](https://tio.run/##VYyxDoIwAER3vuJCIB0IBNjEBmOibkriqg4EWmBpSVsSg/rtWLoYb7rL3b1Z90tTG5ToQCnIsTqRxah6hE8pDW7X/eVQncMoErusEA8f6XaxG8@oSdgfQ6wRZ@g8LhUGDAKvLEk2ny1a6cFqhXcoS3AXRzUIw0HyFJagITnC9gktJ9WwAgR@MPhu@QfM0x9x1ax7cLxhFMhdEPsjrmulYM6wppeeS8sX "Zsh – Try It Online")
```
trap "<<<$[RANDOM%++n?1:n]" 0;
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
‘ɼḷXỊ®*
```
[Try it online!](https://tio.run/##ARgA5/9qZWxsef//4oCYybzhuLdY4buKwq4q//8 "Jelly – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 7 bytes
```
¾Å1¼¾ªΩ
```
[Try it online!](https://tio.run/##yy9OTMpM/f//0L7DrYaH9hzad2jVuZW4Of//AwA "05AB1E – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), 78 76 75 bytes
Using the same trick as in the [link](https://codegolf.stackexchange.com/questions/132558/i-double-the-source-you-double-the-output) 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!](https://tio.run/##K6gsycjPM/7/P60oP1ehKDEvBUhl5hbkF5VoWefZ5qTmaWjlFwDJ@Pi0zJzU@HhNTX19c1PrgqLMvBINQ@063TwtDYg2DU0bQ/08TU3l//8B "Python 3 – 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
[Answer]
# Dyalog APL, ~~25~~ ~~24~~ ~~23~~ ~~22~~ 21 bytes
```
0{×⍺:1+⍵⋄⎕←1⌈⍵×1=?⍵}1
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///36D68PRHvbusDLUf9W591N3yqG/qo7YJho96OoD8w9MNbe2BdK3h//8A "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), ~~17~~ ~~15~~ ~~14~~ 13 bytes
```
Øgl13÷:(1w&+ṛ
```
[Try it online!](https://tio.run/##S0/MTPz///CM9BxD48PbrTQMy9W0H@6c/f8/AA "Gaia – Try It Online")
I randomly noticed the behavior of `Øg` yesterday when looking through the docs, which helped immensely.
[Answer]
# Perl 5, ~~28~~ 26 bytes
-2 bytes thanks to @Grimy
```
1 if!++$x;say 1<rand$x||$x
```
[TIO](https://tio.run/##K0gtyjH9/99QITNNUVtbpcK6OLFSwdCmKDEvRaWipkalghyZ////5ReUZObnFf/X9TXVMzAEAA)
[Answer]
# [Ruby](https://www.ruby-lang.org/), 40 bytes
```
1
n||=@x=0
n+=1
puts 1>rand(n)?n:1 if @x
```
[Try it online!](https://tio.run/##KypNqvz/35Arr6bG1qHC1oArT9vWkKugtKRYwdCuKDEvRSNP0z7PylAhM03BoeL/fwA "Ruby – Try It Online")
[Try it online (copied 3 times)!](https://tio.run/##KypNqvz/35Arr6bG1qHC1oArT9vWkKugtKRYwdCuKDEvRSNP0z7PylAhM03BoYLa6v7/BwA)
A ruby port of this [Python answer](https://codegolf.stackexchange.com/questions/191393/i-multiply-the-source-you-probably-multiply-the-output/191429#191429).
# [Ruby](https://www.ruby-lang.org/), 38 bytes
2 bytes saved by reading the file:
```
n=File.size($0)/38;puts 1>rand(n)?n:1#
```
[Try it online!](https://tio.run/##KypNqvz/P8/WLTMnVa84sypVQ8VAU9/YwrqgtKRYwdCuKDEvRSNP0z7PylD5/38A "Ruby – Try It Online")
[Answer]
# [Runic Enchantments](https://github.com/Draco18s/RunicEnchantments/tree/Console), 31 bytes
```
UwR'10<;$\
I+:'RA0)?/1$;
1
l;
y
```
[Try it online!](https://tio.run/##KyrNy0z@/z@0PEjd0MDGWiWGy1PbSj3I0UDTXt9QxZrLkCvHmqvy/38A "Runic Enchantments – Try It Online")
Uses the same structure as [this answer](https://codegolf.stackexchange.com/a/178761/47990) to count how many times the source has been duplicated:
[![Execution flow](https://i.stack.imgur.com/zaEag.png)](https://i.stack.imgur.com/zaEag.png)
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.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~9~~ 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
(°Tö)ÎªT
```
[Test it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=KLBU9inOqlQ) | [Doubled](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=KLBU9inOqlQosFT2Kc6qVA) | [Tripled](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=KLBU9inOqlQosFT2Kc6qVCiwVPYpzqpU)  
[Verify distribution of 10000 runs after 10 repetitions](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=KLBU9inOqlQosFT2Kc6qVCiwVPYpzqpUKLBU9inOqlQosFT2Kc6qVCiwVPYpzqpUKLBU9inOqlQosFT2Kc6qVCiwVPYpzqpUKLBU9inOqlQ&footer=VPZMsiCuzqpUw/wgrs4rIiA9ICIrWmzDcVI)
```
(°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, ~~13~~ ~~11~~ ~~10~~ 9 bytes
Note the trailing space.
```
NoÎp°T ö 
```
[Test it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Tm/OcLBUIPYg) | [Doubled](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Tm/OcLBUIPYgTm/OcLBUIPYg) | [Tripled](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Tm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYg)  
[Verify distribution of 10000 runs after 10 repetitions](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Tm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYgTm/OcLBUIPYg&footer=TvZMsiD8IK7OKyIgPSAiK1psw3FS)
```
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
```
[Answer]
# JavaScript ([JavaScript shell 71](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell)), 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.)
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), ~~133~~ ~~114~~ 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!](https://tio.run/##FcuxCsIwEADQ/b7iJEtCsdDVWB2cuji4OIfkAgf1EryAguivR93fi7qNyr0bzpsFTKLMQriAttA4IktD9hDXoIr19anWzYdTES0rjdc7N7JCD7wESeVm3XimZ7Ps9tORd5Pz74o6/8HveTAkiTPwMHjo/Qs "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 12 bytes
```
⎚Ｉ⎇‽Ｌ⊞Ｏυω¹Ｌυ
```
[Try it online!](https://tio.run/##ASwA0/9jaGFyY29hbP//4o6a77yp4o6H4oC977ys4oqe77yvz4XPicK577ysz4X//w "Charcoal – 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
```
]